JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1897901
  • Score
    100M100P100Q203780F
  • License MIT

Fastest memoization lib that supports N arguments

Package Exports

  • fast-memoize

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (fast-memoize) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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.

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.

Benchmark

For an in depth explanation on how this library was created, go read this post on RisingStack.

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

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

Credits


caiogondim.com  ·  GitHub @caiogondim  ·  Twitter @caio_gondim