aboutsummaryrefslogtreecommitdiffstats
path: root/2623
diff options
context:
space:
mode:
authorterminaldweller <devi@terminaldweller.com>2024-01-05 21:56:35 +0000
committerterminaldweller <devi@terminaldweller.com>2024-01-05 21:56:35 +0000
commite2c8959107150f8bf7073c8ad767663e87dcc356 (patch)
tree331c1ebfc751a76a1f4aac13c25667f65a8da26f /2623
parent2666 (diff)
downloadleetcode-e2c8959107150f8bf7073c8ad767663e87dcc356.tar.gz
leetcode-e2c8959107150f8bf7073c8ad767663e87dcc356.zip
2623
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
+ */