Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including memoize-id with all npm packages installed. Try it out:

var memoizeId = require("memoize-id")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

memoize-id v1.0.0

Memoize a function by the identity of its arguments, using ES2015 Map

memoize-id npm

Memoize a function by the identity of its arguments, using ES2015 Map.

Installation

npm install memoize-id

Note: ES2015 Symbol and Map must be available where memoize-id is used. Refer to ECMAScript 6 compatibility table.

Usage

import memoize from 'memoize-id';
// or:
// const memoize = require('memoize-id').default;

let i = 0;
function fn(foo, bar, baz) {
  i += 1;
  return {
    args: [foo, bar, baz],
    i,
  };
}
const memoizedFn = memoize(fn);

console.log(i);
// 0
const v1 = memoizedFn('foo', 'bar', 'baz');
console.log(v1);
// { args: ['foo', 'bar', 'baz'], i: 1 }
console.log(i);
// 1
const v2 = memoizedFn('foo', 'bar', 'baz');
console.log(v2);
// { args: ['foo', 'bar', 'baz'], i: 1 }
console.log(v1 === v2);
// true
console.log(i);
// 1
const v3 = memoizedFn('foo', 'qux', 'baz');
console.log(v3);
// { args: ['foo', 'qux', 'baz'], i: 2 }
console.log(i);
// 2
console.log(v1 == v3);
// false
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free