memoizejs

2018-08-18 admin

memoizejs是什么

什么是memoizejs,A faster JavaScript memoizer

memoizejs介绍、memoizejs使用

A faster JavaScript memoizer

Whilst not new by any means, memoization is a useful optimization technique for caching the results of function calls such that lengthy lookups or expensive recursive computations can be minimized where possible.

The basic idea is that if you can detect an operation has already been previously completed for a specific set of input values, the stored result can instead be returned instead of repeating the operation again.

Some of the problems memoization can help optimize include: recursive mathematics, algorithmic caching of canvas animation and more generally, any problems which can be expressed as a set of calls to the same function with a combination of argument values that repeat.

Usage

Imagine having a function you want to memoize, like running the Fibbonacci sequence:

var fib, fiborg;

fiborg = fib = function (x) {
  if(x < 2) return 1; else return fib(x-1) + fib(x-2);
}

We can then memoize it as follows:

fib = memoize(fiborg);
fib(20);

License

Released under an MIT license.

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-38308.html

文章标题:memoizejs

回到顶部