You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
651 B
26 lines
651 B
/* |
|
* memoize.js |
|
* by @philogb and @addyosmani |
|
* with further optimizations by @mathias |
|
* and @DmitryBaranovsk |
|
* perf tests: http://bit.ly/q3zpG3 |
|
* Released under an MIT license. |
|
*/ |
|
module.exports = function memoize(fn) { |
|
return function(...args) { |
|
let index = args.length, |
|
hash = '', |
|
currentArg = null; |
|
|
|
currentArg = null; |
|
|
|
while (index--) { |
|
currentArg = args[index]; |
|
hash += currentArg === Object(currentArg) ? JSON.stringify(currentArg) : currentArg; |
|
|
|
fn.memoize || (fn.memoize = {}); |
|
} |
|
|
|
return hash in fn.memoize ? fn.memoize[hash] : (fn.memoize[hash] = fn.apply(this, args)); |
|
}; |
|
};
|
|
|