Comparing version 5.4.1 to 5.5.0
{ | ||
"name": "utilsac", | ||
"version": "5.4.1", | ||
"version": "5.5.0", | ||
"description": "tools mostly", | ||
@@ -5,0 +5,0 @@ "main": "files.js", |
@@ -36,3 +36,4 @@ # Utilities and random js files | ||
timeCallback, | ||
timePromise | ||
timePromise, | ||
memoizeAsStrings | ||
} from "path.../utility.js"; | ||
@@ -39,0 +40,0 @@ ``` |
@@ -10,3 +10,4 @@ export { | ||
timeCallback, | ||
timePromise | ||
timePromise, | ||
memoizeAsStrings | ||
}; | ||
@@ -173,1 +174,27 @@ | ||
}; | ||
const memoizeAsStrings = function (functionToMemoize) { | ||
/* | ||
todo explain better the limitations and benefits of this approach | ||
joins together the args as strings to compare | ||
false possible cache hits when "-" is inside the string | ||
*/ | ||
const separator = "-"; | ||
const previousResults = {}; | ||
return function (...args) { | ||
const argumentsAsStrings = args.map(String).join(separator); | ||
/* | ||
without .map(String) works but undefined and null become empty strings | ||
const argumentsAsStrings = args.join(separator); | ||
*/ | ||
if (Object.prototype.hasOwnProperty.call(previousResults, argumentsAsStrings)) { | ||
// cache hit | ||
return previousResults[argumentsAsStrings]; | ||
} | ||
// not yet in cache | ||
const result = functionToMemoize(...args); | ||
// add it for later | ||
previousResults[argumentsAsStrings] = result; | ||
return result; | ||
}; | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18146
295
61