We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents e71edf2 + 0547031 commit 0441e9dCopy full SHA for 0441e9d
javascript/2623-memoize.js
@@ -0,0 +1,24 @@
1
+/**
2
+ * @param {Function} fn
3
+ * @return {Function}
4
+ */
5
+function memoize(fn) {
6
+ let memo = {};
7
+ return function (...args) {
8
+ let argsjson = JSON.stringify(args);
9
+ if (memo.hasOwnProperty(argsjson))
10
+ return memo[argsjson];
11
+ return memo[argsjson] = fn(...args);
12
+ }
13
+}
14
+
15
16
+ * let callCount = 0;
17
+ * const memoizedFn = memoize(function (a, b) {
18
+ * callCount += 1;
19
+ * return a + b;
20
+ * })
21
+ * memoizedFn(2, 3) // 5
22
23
+ * console.log(callCount) // 1
24
0 commit comments