A utility to provide LRU memoization for any js function
Clone or download
erikras Merge pull request #86 from dlindenkreuz/typescript-defs
Add typescript type definition file
Latest commit a9b4b3d Feb 27, 2018



NPM Version NPM Downloads Build Status

lru-memoize is a utility to provide simple memoization for any pure javascript function, using an LRU cache that prioritizes the most recently accessed values, and discards the "least recently used" (LRU) items when the size limit is reached. If your function has side effects or relies on some external state to generate its result, it should not be memoized.


npm install --save lru-memoize


Let's look at an example where we want to memoize a function that multiplies three numbers together, and we want to keep the last ten arguments -> value mappings in memory.


var memoize = require('lru-memoize');

var multiply = function(a, b, c) {
  return a * b * c;

multiply = memoize(10)(multiply);

module.exports = multiply;


import memoize from 'lru-memoize';

let multiply = (a, b, c) => a * b * c;

multiply = memoize(10)(multiply);

export default multiply;


memoize(limit:Integer?, equals:Function?, deepObjects:Boolean?)

Returns (Function) => Function.

-limit : Integer [optional]

The number of arguments -> value mappings to keep in memory. Defaults to 1.

-equals : Function [optional]

A function to compare two values for equality. Defaults to ===.

-deepObjects : Boolean [optional]

Whether or not to perform a deep equals on Object values. Defaults to false.