MyGit

caiogondim/fast-memoize.js

Fork: 86 Star: 2579 (更新于 2024-11-29 01:41:38)

license: MIT

Language: JavaScript .

:rabbit2: Fastest possible memoization library

最后发布版本: v2.4.0 ( 2018-06-05 04:07:17)

官方网址 GitHub网址

fast-memoize

 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

npm install fast-memoize --save

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 git@github.com: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

Rest & Default Parameters

We check for function.length to get upfront the expected number of arguments in order to use the fastest strategy. But when rest & default parameters are being used, we don't receive the right number of arguments (see details).

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

// Default parameter example
function divide (element, divisor = 1) {
  return divisor * element
}
divide.length // => 1

So if you use rest & default parameters, explicitly set the strategy to variadic.

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

Function Arguments

The default serializer uses JSON.stringify which will serialize functions as null. This means that if you are passing any functions as arguments you will get the same output regardless of whether you pass in different functions or indeed no function at all. The cache key generated will always be the same. To get around this you can give each function a unique ID and use that.

let id = 0
function memoizedId(x) {
  if (!x.__memoizedId) x.__memoizedId = ++id
  return { __memoizedId: x.__memoizedId }
}

memoize((aFunction, foo) => {
  return aFunction.bind(foo)
}, {
  serializer: args => {
    const argumentsWithFuncIds = Array.from(args).map(x => {
      if (typeof x === 'function') return memoizedId(x)
      return x
    })
    return JSON.stringify(argumentsWithFuncIds)
  }
})

Credits


caiogondim.com  ·  GitHub @caiogondim  ·  Twitter @caio_gondim

最近版本更新:(数据更新于 2024-09-17 00:35:54)

2018-06-05 04:07:17 v2.4.0

2018-02-07 07:35:55 v2.3.0

2017-08-03 01:33:29 v2.2.8

2017-03-20 22:28:29 v2.2.7

2017-03-17 00:03:18 v2.2.3

2017-02-02 19:26:21 v2.2.1

2017-02-02 19:25:33 v2.2.0

2017-01-26 22:13:21 v2.1.1

2017-01-26 21:58:46 v2.1.0

主题(topics):

javascript, js, memoization

caiogondim/fast-memoize.js同语言 JavaScript最近更新仓库

2024-12-22 13:19:55 gethomepage/homepage

2024-12-22 11:34:40 chris81605/Degrees-of-Lewdity_Cheat_Extended

2024-12-22 00:44:14 gorhill/uBlock

2024-12-21 12:14:02 layui/layui

2024-12-21 03:44:01 emberjs/ember.js

2024-12-20 02:57:09 nodejs/node