GithubHelp home page GithubHelp logo

micahstubbs / fast-memoize.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from caiogondim/fast-memoize.js

0.0 1.0 1.0 1.19 MB

:rabbit2: Fastest possible memoization library

License: MIT License

JavaScript 98.96% Shell 1.04%

fast-memoize.js's Introduction

fast-memoize.js

 Travis CI Code coverage

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 environment, but it is possible to pass a custom cache to be used.

const memoized = memoize(fn, {
  cache: {
    create() {
      var store = {};
      return {
        has(key) { return (key in store); },
        get(key) { return store[key]; },
        set(key, value) { store[key] = value; }
      };
    }
  }
})

The custom cache should be an object containing a create method that returns an object implementing the following methods:

  • get
  • set
  • has

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 returns 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 [email protected]:caiogondim/fast-memoize.git
cd fast-memoize
npm install
npm run benchmark

Against another git hash

To benchmark the current code against a git hash, branch, ...

npm run benchmark:compare 53fa9a62214e816cf8b5b4fa291c38f1d63677b9

Gotchas

Spread arguments

We check for function.length to get upfront the expected number of arguments in order to use the fastest strategy. But with spread arguments we don't receive the right number.

function multiply (multiplier, ...theArgs) {
  return theArgs.map(function (element) {
    return multiplier * element
  })
}
multiply.length // => 1

So if you use spread arguments, explicitly set the strategy to variadic.

const memoizedMultiply = memoize(multiply, {
  strategy: memoize.strategies.variadic
})

Credits


caiogondim.com  ·  GitHub @caiogondim  ·  Twitter @caio_gondim

fast-memoize.js's People

Contributors

caiogondim avatar aladdin-add avatar amilajack avatar allenfang avatar aduth avatar comerc avatar benfogel avatar getify avatar leobalter avatar piperchester avatar rafael-lima avatar ruyadorno avatar larkinscott avatar cap32 avatar elliotthill avatar slavaganzin avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.