New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@zherdev/ts-cache

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@zherdev/ts-cache

A typescript decorator for pure methods which improves your performance.

latest
Source
npmnpm
Version
1.0.2
Version published
Maintainers
1
Created
Source

@Cache()

A decorator for pure methods which improves performance

If you have a complicated method which is called many times and takes a long time you can just add @Cache() decorator to it.

import { Cache } from '@zherdev/ts-cache';

class ExampleClass {

    @Cache()
    method(count: number): number {
        console.log(`Execution with value: ${count}`);
        let sum = 0;
        for (let i = 0; i < count; i++) {
            sum++;        
        }
        
        return sum;
    }

}

const obj = new ExampleClass();

const res1 = obj.method(1000000); // Takes some time
const res2 = obj.method(1000000); // Instant execution
const res3 = obj.method(9999999); // Takes some time

console.log(res1, res2);
console.log(`res1: ${res1}`);
console.log(`res2: ${res2}`);
console.log(`res3: ${res3}`);

/* Console output
   Execution with value: 1000000
   Execution with value: 9999999
   res1: 1000000
   res2: 1000000
   res3: 9999999
*/

You can see that obj.method() was called twice but executed only once. Anyway we have the same result in both variables: res1 and res2. It's because every time when you call your method with the same arguments as once before it returns result instantly. @Cache() decorator just keeps it in memory.

Be careful with @Cache() decorator. It may cause some unexpected behavior if your method need to be executed every time it's called.

FAQs

Package last updated on 31 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts