Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Typescript memoize decorator with expiration time
import {memoize, clearMemoizedValue} from "./memoize";
describe("memoize", () => {
class MyClass {
getNumber() {
return Math.random();
let a: MyClass;
beforeEach(() => {
a = new MyClass;
it("memoizes the value", () => {
it("allows to manually clear the value", () => {
let first = a.getNumber();
it("clears the value automatically after timeout", (done) => {
let first = a.getNumber();
setTimeout(() => {
}, 6);
const MEMOIZED_VALUE_KEY = '_memoizedValue';
export function memoize(expirationTimeMs: number = 60000) {
return (target: any, propertyName: string, descriptor: TypedPropertyDescriptor<any>) => {
if (descriptor.value != null) {
const originalMethod = descriptor.value;
let fn = function (...args: any[]) {
fn[MEMOIZED_VALUE_KEY] = originalMethod.apply(this, args);
setTimeout(() => clearMemoizedValue(fn), expirationTimeMs);
descriptor.value = fn;
return descriptor;
else {
throw "Only put the @memoize decorator on a method.";
export function clearMemoizedValue(method) {
delete method[MEMOIZED_VALUE_KEY];

This comment has been minimized.

Copy link
Owner Author

@fracz fracz commented Jan 14, 2017

Idea based on the memoize-decorator.ts by @dsherret. Can be perfectly used with promises and API requests.

It stores the memoized value as the method attribute (the original stores the value in the object that owns the method).

It allows to clear the memoized value manually when it needs to be recalculated (clearMemoizedValue(someService.someAnnotatedMethod)). It also clears the value automatically after the specified time so the next call will recalculate it.

The following code with memoize (cache) the returned value for 5 seconds.

findAllEntities() { ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment