aboutsummaryrefslogtreecommitdiffstats
path: root/2623
diff options
context:
space:
mode:
Diffstat (limited to '2623')
-rw-r--r--2623/main.js30
1 files changed, 30 insertions, 0 deletions
diff --git a/2623/main.js b/2623/main.js
new file mode 100644
index 0000000..966025f
--- /dev/null
+++ b/2623/main.js
@@ -0,0 +1,30 @@
+#!/usr/bin/env node
+"use strict";
+
+/**
+ * @param {Function} fn
+ * @return {Function}
+ */
+function memoize(fn) {
+ const cache = {};
+ return function memoized(...args) {
+ const key = JSON.stringify(args);
+ if (key in cache) {
+ return cache[key];
+ }
+ const result = fn(...args);
+ cache[key] = result;
+ return result;
+ };
+}
+
+/**
+ * let callCount = 0;
+ * const memoizedFn = memoize(function (a, b) {
+ * callCount += 1;
+ * return a + b;
+ * })
+ * memoizedFn(2, 3) // 5
+ * memoizedFn(2, 3) // 5
+ * console.log(callCount) // 1
+ */