Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Caches the return value of get accessors and methods.
*
* Notes:
* - Doesn't really make sense to put this on a method with parameters.
* - Creates an obscure non-enumerable property on the instance to store the memoized value.
* - Could use a WeakMap, but this way has support in old environments.
*/
export function Memoize(target: any, propertyName: string, descriptor: TypedPropertyDescriptor<any>) {
if (descriptor.value != null) {
descriptor.value = getNewFunction(descriptor.value);
}
else if (descriptor.get != null) {
descriptor.get = getNewFunction(descriptor.get);
}
else {
throw "Only put a Memoize decorator on a method or get accessor.";
}
}
let counter = 0;
function getNewFunction(originalFunction: () => void) {
const identifier = ++counter;
return function (this: any, ...args: any[]) {
const propName = `__memoized_value_${identifier}`;
let returnedValue: any;
if (this.hasOwnProperty(propName)) {
returnedValue = this[propName];
}
else {
returnedValue = originalFunction.apply(this, args);
Object.defineProperty(this, propName, {
configurable: false,
enumerable: false,
writable: false,
value: returnedValue
});
}
return returnedValue;
};
}
@dsherret

This comment has been minimized.

Copy link
Owner Author

commented Oct 29, 2015

Example:

import * as assert from "assert";
import {Memoize} from "./memoize-decorator";

describe("Memoize", () => {
    class MyClass {
        @Memoize
        getNumber() {
            return Math.random();
        }

        @Memoize
        get value() {
            return Math.random();
        }
    }

    const a = new MyClass();
    const b = new MyClass();

    it("method should be memoized", () => {
        assert.equal(a.getNumber(), a.getNumber());
    });

    it("accessor should be memoized", () => {
        assert.equal(a.value, a.value);
    });

    it("multiple instances shouldn't share values for methods", () => {
        assert.notEqual(a.getNumber(), b.getNumber());
    });

    it("multiple instances shouldn't share values for accessors", () => {
        assert.notEqual(a.value, b.value);
    });
});
@timjroberts

This comment has been minimized.

Copy link

commented May 19, 2016

Great example, however, I think you're memoizing values across all instances of a class rather than per instance. For example, given the following two instances:

const c1 = new MyClass();
const c2 = new MyClass();

Calling getNumber() should memoize the returned value once for c1 and once for c2. Currently, because TypeScript only applies the decorator once, the decoration is shared across all instances. This would obviously be the expected behaviour if getNumber() were static.

@dsherret

This comment has been minimized.

Copy link
Owner Author

commented Aug 1, 2016

@timjroberts Hi Tim, you are completely right and sorry about that! It did share the same value across multiple instances. I wish gist had given me a notification for your message, because I didn't noticed this mistake until I went to go use this in something and it broke the tests.

I went ahead and updated this so that problem is fixed. Additionally, I've updated the tests in this post to cover that situation.

@torabian

This comment has been minimized.

Copy link

commented Dec 27, 2016

I am looking a method to find "any assigned decorator to properties within a class", for example:

class MyClass {
        @Memoize
        getNumber() {
            return Math.random();
        }

        @Memoize @rand
        get value() {
            return Math.random();
        }
    }

I want to get @memoize for getNumber:

let result = Reflect.GetDecorations(MyClass);
console.log(result);
// { value: ['Memoize' , 'rand'] , getNumber: ['Memoize'] }

Is there any possible solution?

@connor4312

This comment has been minimized.

Copy link

commented Jan 2, 2017

You could use a WeakMap to avoid storing the magical property 😄

@fracz

This comment has been minimized.

Copy link

commented Jan 15, 2017

Thank you for this example!
I have created similar memoize decorator that can be expired after some time.

@SteveStrong

This comment has been minimized.

Copy link

commented Mar 19, 2018

Just wondering, how is this different from a static property

@natew

This comment has been minimized.

Copy link

commented Jul 25, 2018

@SteveStrong in certain cases where you want to use the prototype this is useful.

@prestongarno

This comment has been minimized.

Copy link

commented Oct 30, 2018

type hashCode = "hashCode";

/**
 * Indicates that this hashcode function should be memoized (calculated only once)
 * ***This should only be used on READONLY composite data types for predictable results***
 */
function memoized(target: ValueType, propertyKey: hashCode, descriptor: TypedPropertyDescriptor<() => number>) {

  const original = descriptor.get ? descriptor.get() : descriptor.value;

  Object.defineProperty(target.constructor.prototype, 'hashCode', {
    value: function hashCode() {
      if (!this.__hashCode__) {
        this.__hashCode__ = original.call(this);
      }
      return this.__hashCode__;
    }
  });

  descriptor.value = target.constructor.prototype.hashCode;

  return descriptor;
}

I found this to work - no WeakMap needed.

This might not work as well if you want to dynamically memoize things, but I am using it to memoize hashcode computations for immutable ValueTypes with Immutable.js

Edit: I am not sure how well this would work with sub-classes overriding a memoized function who also decorates the overridden function to memoize the calculation, it might loop infinitely?

@smnbbrv

This comment has been minimized.

Copy link

commented May 24, 2019

Here is a WeakMap based version of this decorator, that respects single argument (by a reference for non-primitives)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.