|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
707 bytes
|
| Line | |
|---|
| 1 | function memoize(func, resolver) {
|
|---|
| 2 | if (typeof func !== 'function' || (resolver != null && typeof resolver !== 'function')) {
|
|---|
| 3 | throw new TypeError('Expected a function');
|
|---|
| 4 | }
|
|---|
| 5 | const memoized = function (...args) {
|
|---|
| 6 | const key = resolver ? resolver.apply(this, args) : args[0];
|
|---|
| 7 | const cache = memoized.cache;
|
|---|
| 8 | if (cache.has(key)) {
|
|---|
| 9 | return cache.get(key);
|
|---|
| 10 | }
|
|---|
| 11 | const result = func.apply(this, args);
|
|---|
| 12 | memoized.cache = cache.set(key, result) || cache;
|
|---|
| 13 | return result;
|
|---|
| 14 | };
|
|---|
| 15 | const CacheConstructor = memoize.Cache || Map;
|
|---|
| 16 | memoized.cache = new CacheConstructor();
|
|---|
| 17 | return memoized;
|
|---|
| 18 | }
|
|---|
| 19 | memoize.Cache = Map;
|
|---|
| 20 |
|
|---|
| 21 | export { memoize };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.