generated from TodePond/Frogasaurus
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from enbyautumn/main
Add MEMOIZE function to automatically memoize a function
- Loading branch information
Showing
6 changed files
with
188 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//=========// | ||
// Memoize // | ||
//=========// | ||
{ | ||
// Memoize the function - Modified from https://tjinlag.medium.com/memoize-javascript-function-638f3b7c80e9 | ||
// keymaker is optional and allows you to specifiy a method for generating the key for the cache | ||
// It should be used if the default method is not suitable for the use case | ||
// The memo function returns a new function that will now be memoized, meaning it caches results | ||
Habitat.memo = (fn, keyMaker) => { | ||
// Make a Map (similar to an object but with faster lookup) to store the results of the function | ||
const cache = new Map() | ||
|
||
return (...args) => { | ||
// Make a key that will be used to look up the results of the function | ||
// This keyMaker is not very efficent, but should work for more use cases | ||
// A better keyMaker would be one that is specific to the use case, such as (a) => args[0] | ||
const key = keyMaker ? keyMaker.apply(this, args) : args.map(JSON.stringify).join('') | ||
|
||
// If the key is in the cache, return the value | ||
if (cache.has(key)) { | ||
return cache.get(key) | ||
} | ||
|
||
// If the key is not in the cache, run the function and store the result in the cache | ||
const result = fn.apply(this, args) | ||
cache.set(key, result) | ||
|
||
return result | ||
} | ||
} | ||
|
||
Habitat.memo.install = (global) => { | ||
global.memo = Habitat.memo | ||
Habitat.memo.installed = true | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters