TGViewer
JavaScript JavaScript @javascript · 31.1K subscribers
Post #2988 3.62K
CHALLENGE

function memoize(fn) {
const cache = new Map();
return function(...args) {
const key = JSON.stringify(args);
if (cache.has(key)) {
return cache.get(key);
}
const result = fn.apply(this, args);
cache.set(key, result);
return result;
};
}

const fibonacci = memoize(function(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
});

console.log(fibonacci(10));
console.log(fibonacci.cache?.size || 'undefined');
  • 🔥 5
  • 👍 3
  • ❤ 1
More from @javascript
  1. Sep 26, 2026Post #3520
  2. Sep 26, 2026CHALLENGE const obj = { value: 42, getValue() { return this?.value; }, getValueArrow: () =…
  3. Sep 25, 2026Post #3518
  4. Sep 25, 2026CHALLENGE let counter = 0; const mod = { get counter() { return counter; }, increment() {…
  5. Sep 24, 2026Post #3516
  6. Sep 24, 2026CHALLENGE class Base { static count = 0; static increment() { this.count++; return this.co…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →