loaders.gl
Version:
Framework-independent loaders for 3D graphics formats
53 lines (43 loc) • 1.23 kB
JavaScript
function isEqual(a, b) {
if (a === b) {
return true;
}
if (Array.isArray(a)) {
// Special treatment for arrays: compare 1-level deep
// This is to support equality of matrix/coordinate props
var len = a.length;
if (!b || b.length !== len) {
return false;
}
for (var i = 0; i < len; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
return false;
}
/**
* Speed up consecutive function calls by caching the result of calls with identical input
* https://en.wikipedia.org/wiki/Memoization
* @param {function} compute - the function to be memoized
*/
export default function memoize(compute) {
var cachedArgs = null;
var cachedResult = null;
return function () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var needsRecompute = !cachedArgs || args.length !== cachedArgs.length || args.some(function (a, i) {
return !isEqual(a, cachedArgs[i]);
});
if (needsRecompute) {
cachedResult = compute.apply(void 0, args);
cachedArgs = args;
}
return cachedResult;
};
}
//# sourceMappingURL=memoize.js.map