npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@blumintinc/typescript-memoize

v1.2.0

Published

Memoize decorator for Typescript with deep equality

Readme

typescript-memoize

npm GitHub license Test

A memoize decorator for Typescript.

Installation

npm install --save typescript-memoize

Usage:

@Memoize(hashFunction?: (...args: any[]) => any)

You can use it in several ways:

  • Memoize a get accessor,
  • Memoize a method which takes no parameters,
  • Memoize a method which varies based on its first parameter only,
  • Memoize a method which varies based on some combination of parameters
  • Memoize a method using objects with deep equality comparison (default)

You can call memoized methods within the same class, too. This could be useful if you want to memoize the return value for an entire data set, and also a filtered or mapped version of that same set.

Memoize a get accessor, or a method which takes no parameters

These both work the same way. Subsequent calls to a memoized method without parameters, or to a get accessor, always return the same value.

I generally consider it an anti-pattern for a call to a get accessor to trigger an expensive operation. Simply adding Memoize() to a get allows for seamless lazy-loading.

import {Memoize,MemoizeExpiring} from 'typescript-memoize';

class SimpleFoo {

    // Memoize a method without parameters
    @Memoize()
    public getAllTheData() {
       // do some expensive operation to get data
       return data;
    }

    // Memoize a method and expire the value after some time in milliseconds
    @MemoizeExpiring(5000)
    public getDataForSomeTime() {
        // do some expensive operation to get data
        return data;
    }

    // Memoize a getter
    @Memoize()
    public get someValue() {
        // do some expensive operation to calculate value
        return value;
    }

}

And then we call them from somewhere else in our code:

let simpleFoo = new SimpleFoo();

// Memoizes a calculated value and returns it:
let methodVal1 = simpleFoo.getAllTheData();

// Returns memoized value
let methodVal2 = simpleFoo.getAllTheData();

// Memoizes (lazy-loads) a calculated value and returns it:
let getterVal1 = simpleFoo.someValue;

// Returns memoized value
let getterVal2 = simpleFoo.someValue;

Memoize a method which varies based on its first parameter only

Subsequent calls to this style of memoized method will always return the same value.

I'm not really sure why anyone would use this approach to memoize a method with more than one parameter, but it's possible.

import {Memoize} from 'typescript-memoize';

class ComplicatedFoo {

	// Memoize a method without parameters (just like the first example)
	@Memoize()
	public getAllTheData() {
		// do some expensive operation to get data
		return data;
	}

	// Memoize a method with one parameter
	@Memoize()
	public getSomeOfTheData(id: number) {
		let allTheData = this.getAllTheData(); // if you want to!
		// do some expensive operation to get data
		return data;
	}

	// Memoize a method with multiple parameters
	// Only the first parameter will be used for memoization
	@Memoize()
	public getGreeting(name: string, planet: string) {
		return 'Hello, ' + name + '! Welcome to ' + planet;
	}

}

We call these methods from somewhere else in our code:

let complicatedFoo = new ComplicatedFoo();

// Returns calculated value and memoizes it:
let oneParam1 = complicatedFoo.getSomeOfTheData();

// Returns memoized value
let oneParam2 = complicatedFoo.getSomeOfTheData();

// Memoizes a calculated value and returns it:
// 'Hello, Darryl! Welcome to Earth'
let greeterVal1 = complicatedFoo.getGreeting('Darryl', 'Earth');

// Ignores the second parameter, and returns memoized value
// 'Hello, Darryl! Welcome to Earth'
let greeterVal2 = complicatedFoo.getGreeting('Darryl', 'Mars');

Memoize a method which varies based on some combination of parameters

Pass in a hashFunction which takes the same parameters as your target method, to memoize values based on all parameters, or some other custom logic

import {Memoize} from 'typescript-memoize';

class MoreComplicatedFoo {

	// Memoize a method with multiple parameters
	// Memoize will remember values based on keys like: 'name;planet'
	@Memoize((name: string, planet: string) => {
		return name + ';' + planet;
	})
	public getBetterGreeting(name: string, planet: string) {
		return 'Hello, ' + name + '! Welcome to ' + planet;
	}
	
	// Memoize based on some other logic
	@Memoize(() => {
		return new Date();
	})
	public memoryLeak(greeting: string) {
		return greeting + '!!!!!';
	}

	// Memoize also accepts parameters via a single object argument
	@Memoize({
		expiring: 10000, // milliseconds
		hashFunction: (name: string, planet: string) => {
			return name + ';' + planet;
		}
	})
	public getSameBetterGreeting(name: string, planet: string) {
		return 'Hello, ' + name + '! Welcome to ' + planet;
	}

}

We call these methods from somewhere else in our code. By now you should be getting the idea:

let moreComplicatedFoo = new MoreComplicatedFoo();

// 'Hello, Darryl! Welcome to Earth'
let greeterVal1 = moreComplicatedFoo.getBetterGreeting('Darryl', 'Earth');

// 'Hello, Darryl! Welcome to Mars'
let greeterVal2 = moreComplicatedFoo.getBetterGreeting('Darryl', 'Mars');

// Fill up the computer with useless greetings:
let greeting = moreComplicatedFoo.memoryLeak('Hello');

Memoize accepts one or more "tag" strings that allow the cache to be invalidated on command

Passing an array with one or more "tag" strings these will allow you to later clear the cache of results associated with methods or the getaccessors using the clear() function.

The clear() function also requires an array of "tag" strings.

import {Memoize} from 'typescript-memoize';

class ClearableFoo {

	// Memoize accepts tags
	@Memoize({ tags: ["foo", "bar"] })
	public getClearableGreeting(name: string, planet: string) {
		return 'Hello, ' + name + '! Welcome to ' + planet;
	}


	// Memoize accepts tags
	@Memoize({ tags: ["bar"] })
	public getClearableSum(a: number, b: number) {
		return a + b;
	}

}

We call these methods from somewhere else in our code.

import {clear} from 'typescript-memoize';

let clearableFoo = new ClearableFoo();

// 'Hello, Darryl! Welcome to Earth'
let greeterVal1 = clearableFoo.getClearableGreeting('Darryl', 'Earth');

// Ignores the second parameter, and returns memoized value
// 'Hello, Darryl! Welcome to Earth'
let greeterVal2 = clearableFoo.getClearableGreeting('Darryl', 'Mars');

// '3'
let sum1 = clearableFoo.getClearableSum(2, 1);

// Ignores the second parameter, and returns memoized value
// '3'
let sum2 = clearableFoo.getClearableSum(2, 2);

clear(["foo"]);

// The memoized values are cleared, return a new value
// 'Hello, Darryl! Welcome to Mars'
let greeterVal3 = clearableFoo.getClearableGreeting('Darryl', 'Mars');


// The memoized value is not associated with 'foo' tag, returns memoized value
// '3'
let sum3 = clearableFoo.getClearableSum(2, 2);

clear(["bar"]);

// The memoized values are cleared, return a new value
// 'Hello, Darryl! Welcome to Earth'
let greeterVal4 = clearableFoo.getClearableGreeting('Darryl', 'Earth');


// The memoized values are cleared, return a new value
// '4'
let sum4 = clearableFoo.getClearableSum(2, 2);

Memoize with deep equality comparison

By default, memoization now uses deep equality when comparing objects, which is useful for objects and arrays with the same structure but different references. If needed, you can disable deep equality with the useDeepEqual: false option:

import {Memoize} from 'typescript-memoize';

class DeepEqualityFoo {
    // Uses deep equality comparison by default
    @Memoize()
    public processObject(obj: any) {
        // This will only be called once for objects with the same structure,
        // even if they are different instances
        return expensiveOperation(obj);
    }

    // Disable deep equality if you want reference equality instead
    @Memoize({
        useDeepEqual: false
    })
    public processObjectWithReferenceEquality(obj: any) {
        // This will be called for each new object reference,
        // even if they have the same structure
        return expensiveOperation(obj);
    }

    // Combine with other options
    @Memoize({
        expiring: 5000, // expire after 5 seconds
        tags: ["objects"]
    })
    public processComplexObject(obj: any) {
        return expensiveOperation(obj);
    }
}

We can call these methods and get memoized results based on deep equality:

let deepFoo = new DeepEqualityFoo();

// First call with an object
const result1 = deepFoo.processObject({ id: 123, name: "Test" });

// Second call with different object instance but same structure
// Will return the memoized result without calling the original method again
const result2 = deepFoo.processObject({ id: 123, name: "Test" });

// Call with different object structure
// Will call the original method again
const result3 = deepFoo.processObject({ id: 456, name: "Different" });

// Deep equality works with complex nested objects too
const result4 = deepFoo.processComplexObject({ 
    user: { 
        id: 123, 
        details: { 
            age: 30, 
            preferences: ["red", "blue"] 
        } 
    }
});

// Using reference equality will call the method for each new object
const refObj = { id: 123, name: "Test" };
const result5 = deepFoo.processObjectWithReferenceEquality(refObj);
// This will be memoized since it's the same reference
const result6 = deepFoo.processObjectWithReferenceEquality(refObj);
// This will call the method again since it's a new reference
const result7 = deepFoo.processObjectWithReferenceEquality({ id: 123, name: "Test" });

The deep equality comparison uses the fast-deep-equal package for efficient deep equality comparisons.