Skip to content

br/fast-memoize.js

 
 

Repository files navigation

fast-memoize.js

Travis CI JS standard style

In computing, memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again. — Wikipedia

This library is an attempt to make the fastest possible memoization library in JavaScript that supports N arguments.

There are already very popular solutions for this problem, but they are not fast enough or accept only one argument.

Installation

To use the library, install it through npm

npm install fast-memoize --save

To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack

Usage

const memoize = require('fast-memoize')

const fn = function (one, two, three) { /* ... */ }

const memoized = memoize(fn)

memoized('foo', 3, 'bar')
memoized('foo', 3, 'bar') // Cache hit

Custom cache

The fastest cache is used for the running enviroment, but it is possible to pass a custom cache to be used.

const memoized = memoize(fn, {
  cache: customCache
})

The custom cache must implement the following methods:

  • get
  • set
  • has
  • delete

Custom serializer

To use a custom serializer:

const memoized = memoize(fn, {
  serializer: customSerializer
})

The serializer is a function that receives one argument and outputs a string that represents it. It has to be a deterministic algorithm meaning that, given one input, it always give the same output.

TTL

You can pass an optional TTL value as an option. After this time when a cache key is retrieved it will be marked as invalid, removed, and the value recomputed.

You can also set data to be automaticaly be removed at the end of the TTL rather than just removed when accessed.

const memoized = memoize(fn, {
  ttl: 120000, // time in ms
  autoExpire: true // Defaults to false.
})

Benchmark

There is already plenty of libraries that does memoization on JS world. underscore and lodash provides it, but they don't accept more than one argument. memoizee is a very well written library that supports N arguments, but is not even close on performance to lodash.

Below you can see a performance benchmark between some of the most popular libraries for memoization.

fast-memoize is faster than any other library but lodash. The reason why is that lodash does not support N arguments and is very optimized to that unique use case. But even though, fast-memoize is the library that supports N that comes closer to it.

To run the benchmark, clone the repo, install the dependencies and run npm run benchmark.

git clone git@github.com:caiogondim/fast-memoize.git
cd fast-memoize
npm install
npm run benchmark

Support

Desktop browsers

Chrome IE Firefox Safari Opera Edge Brave
Latest 8+ Latest Latest Latest Latest Latest

Mobile browsers

| Chrome | Safari | Android Browser | IE | Firefox | Opera | UC | | --- | --- | --- | --- | --- | --- | --- | --- | --- | | Latest | 6+ | 4.0+ | 8+ | Latest | Latest | Latest |

Server

0.10+ ✔

Reference

Credits


caiogondim.com  ·  GitHub @caiogondim  ·  Twitter @caio_gondim

About

🐇 Fastest possible memoization library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 97.0%
  • HTML 3.0%