From 983ed2ba297eb4222c9e2c75f0ee2ee69c4b8b9c Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 2 Jan 2018 13:05:39 +1100 Subject: [PATCH 1/4] Update memoize to use a map and allow access to it --- snippets/memoize.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/snippets/memoize.md b/snippets/memoize.md index c1b6b1343..174812f26 100644 --- a/snippets/memoize.md +++ b/snippets/memoize.md @@ -2,13 +2,16 @@ Returns the memoized (cached) function. -Use `Object.create(null)` to create an empty object without `Object.prototype` (so that those properties are not resolved if the input value is something like `'hasOwnProperty'`). +Create an empty cache using by instantiating a new `Map` object. Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not. +Allow access to the `cache` by setting it as a property on the returned function. ```js const memoize = fn => { - const cache = Object.create(null); - return value => cache[value] || (cache[value] = fn(value)); + const cache = new Map(); + const cached = val => cache.get(val) || (cache.set(val, fn(val))) && cache.get(val); + cached.cache = cache; + return cached; }; ``` @@ -17,4 +20,5 @@ const memoize = fn => { const anagramsCached = memoize(anagrams); anagramsCached('javascript'); // takes a long time anagramsCached('javascript'); // returns virtually instantly since it's now cached +console.log(anagramsCached.cache); // Map ``` From 41ed3f5b0d0f16efa81f6559703d2a110acc7072 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 2 Jan 2018 14:29:08 +1100 Subject: [PATCH 2/4] Update memoize.md --- snippets/memoize.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/memoize.md b/snippets/memoize.md index 174812f26..f945b645d 100644 --- a/snippets/memoize.md +++ b/snippets/memoize.md @@ -2,7 +2,7 @@ Returns the memoized (cached) function. -Create an empty cache using by instantiating a new `Map` object. +Create an empty cache by instantiating a new `Map` object. Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not. Allow access to the `cache` by setting it as a property on the returned function. From 3c54e6a40214f50d19b5e55ec7c530d899b09f4e Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 2 Jan 2018 15:06:52 +1100 Subject: [PATCH 3/4] Update memoize.md --- snippets/memoize.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/memoize.md b/snippets/memoize.md index f945b645d..7df16e542 100644 --- a/snippets/memoize.md +++ b/snippets/memoize.md @@ -9,7 +9,7 @@ Allow access to the `cache` by setting it as a property on the returned function ```js const memoize = fn => { const cache = new Map(); - const cached = val => cache.get(val) || (cache.set(val, fn(val))) && cache.get(val); + const cached = val => cache.has(val) ? cache.get(val) : (cache.set(val, fn(val)) && cache.get(val)); cached.cache = cache; return cached; }; From e3ed2c24b90cd6a55fe0ebfd624c9623ea75822e Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 3 Jan 2018 05:10:16 +1100 Subject: [PATCH 4/4] Update memoize.md --- snippets/memoize.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/snippets/memoize.md b/snippets/memoize.md index 7df16e542..856375a69 100644 --- a/snippets/memoize.md +++ b/snippets/memoize.md @@ -3,13 +3,17 @@ Returns the memoized (cached) function. Create an empty cache by instantiating a new `Map` object. -Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not. +Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not. The `function` keyword must be used in order to allow the memoized function to have its `this` context changed if necessary. Allow access to the `cache` by setting it as a property on the returned function. ```js const memoize = fn => { const cache = new Map(); - const cached = val => cache.has(val) ? cache.get(val) : (cache.set(val, fn(val)) && cache.get(val)); + const cached = function(val) { + return cache.has(val) + ? cache.get(val) + : cache.set(val, fn.call(this, val)) && cache.get(val); + }; cached.cache = cache; return cached; };