mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
32 lines
1002 B
JavaScript
32 lines
1002 B
JavaScript
// function utils
|
|
|
|
/*
|
|
* Memoize a given function by caching the computed result.
|
|
* The cache of a memoized function can be cleared by deleting the `cache`
|
|
* property of the function.
|
|
*
|
|
* @param {function} fn The function to be memoized.
|
|
* Must be a pure function.
|
|
* @param {function(args: Array)} [hasher] A custom hash builder.
|
|
* Is JSON.stringify by default.
|
|
* @return {function} Returns the memoized function
|
|
*/
|
|
exports.memoize = function(fn, hasher) {
|
|
return function memoize() {
|
|
if (typeof memoize.cache !== 'object') {
|
|
memoize.cache = {};
|
|
}
|
|
|
|
var args = [];
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
args[i] = arguments[i];
|
|
}
|
|
|
|
var hash = hasher ? hasher(args) : JSON.stringify(args);
|
|
if (!(hash in memoize.cache)) {
|
|
return memoize.cache[hash] = fn.apply(fn, args);
|
|
}
|
|
return memoize.cache[hash];
|
|
};
|
|
};
|