MCPcopy
hub / github.com/caolan/async / memoize

Function memoize

lib/memoize.js:49–77  ·  view source on GitHub ↗
(fn, hasher)

Source from the content-addressed store, hash-verified

47 * });
48 */
49export default function memoize(fn, hasher) {
50 var memo = Object.create(null);
51 var queues = Object.create(null);
52 hasher = hasher || identity;
53 var _fn = wrapAsync(fn);
54 var memoized = initialParams(function memoized(args, callback) {
55 var key = hasher.apply(null, args);
56 if (has(memo, key)) {
57 setImmediate(function() {
58 callback.apply(null, memo[key]);
59 });
60 } else if (has(queues, key)) {
61 queues[key].push(callback);
62 } else {
63 queues[key] = [callback];
64 _fn.apply(null, args.concat(rest(function(args) {
65 memo[key] = args;
66 var q = queues[key];
67 delete queues[key];
68 for (var i = 0, l = q.length; i < l; i++) {
69 q[i].apply(null, args);
70 }
71 })));
72 }
73 });
74 memoized.memo = memo;
75 memoized.unmemoized = fn;
76 return memoized;
77}

Callers

nothing calls this directly

Calls 3

wrapAsyncFunction · 0.85
hasFunction · 0.85
restFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…