Comparing version
@@ -10,5 +10,6 @@ /** | ||
* Caches the return value of calling a function. Repeat calls | ||
* will simply return the cached value. Pending tests. | ||
* Please report bugs. | ||
* will simply return the cached value. | ||
* | ||
* Pending tests. Please report bugs. | ||
* | ||
* @example | ||
@@ -18,10 +19,13 @@ * | ||
export declare function cache<T extends ICallable>(func: T): T; | ||
declare function identityHash(object: any): any; | ||
/** | ||
* Caches the return value of calling a function. using the arguments as keys. | ||
* This is also called memoization. Please don't abuse this function to avoid | ||
* memory inefficiencies. Pending tests. Please report bugs. | ||
* This is also called memoization. | ||
* | ||
* Pending tests. Please report bugs. | ||
* | ||
* @example | ||
* | ||
*/ | ||
export declare function cacheWith<T extends ICallable>(func: T): T; | ||
export declare function cacheWith<T extends ICallable>(func: T, hash?: typeof identityHash): T; | ||
export {}; |
@@ -12,5 +12,6 @@ "use strict"; | ||
* Caches the return value of calling a function. Repeat calls | ||
* will simply return the cached value. Pending tests. | ||
* Please report bugs. | ||
* will simply return the cached value. | ||
* | ||
* Pending tests. Please report bugs. | ||
* | ||
* @example | ||
@@ -21,6 +22,6 @@ * | ||
let result, called = false; | ||
return Object.assign((...args) => { | ||
return Object.assign(function (...args) { | ||
if (called) | ||
return result; | ||
result = func(...args); | ||
result = func.call(this, ...args); | ||
called = true; | ||
@@ -30,30 +31,31 @@ }, { reset: () => { called = false; } }); | ||
exports.cache = cache; | ||
const hashes = new WeakMap(); | ||
function identityHash(object) { | ||
if (hashes.has(object)) | ||
return hashes.get(object); | ||
else { | ||
const randomHash = `${Math.random()}`; | ||
hashes.set(object, randomHash); | ||
return randomHash; | ||
} | ||
} | ||
/** | ||
* Caches the return value of calling a function. using the arguments as keys. | ||
* This is also called memoization. Please don't abuse this function to avoid | ||
* memory inefficiencies. Pending tests. Please report bugs. | ||
* This is also called memoization. | ||
* | ||
* Pending tests. Please report bugs. | ||
* | ||
* @example | ||
* | ||
*/ | ||
function cacheWith(func) { | ||
let results = []; | ||
return Object.assign((...args) => { | ||
let count, i, arg; | ||
for (let result of results) { | ||
count = 0; | ||
for ([i, arg] of args.entries()) { | ||
if (result[i] === arg) | ||
count++; | ||
else | ||
break; | ||
} | ||
if (count === args.length) | ||
return result[i + 1]; | ||
function cacheWith(func, hash = identityHash) { | ||
let results = {}; | ||
return Object.assign(function (...args) { | ||
const argsHash = args.map(arg => hash(arg)).join('-|-'); | ||
if (!results.hasOwnProperty(argsHash)) { | ||
results[argsHash] = func.call(this, ...args); | ||
} | ||
const result = func(...args); | ||
results.push([...args, result]); | ||
return result; | ||
}, { reset: () => { results = []; } }); | ||
return results[argsHash]; | ||
}, { reset: () => { results = {}; } }); | ||
} | ||
exports.cacheWith = cacheWith; |
@@ -10,5 +10,6 @@ /** | ||
* Caches the return value of calling a function. Repeat calls | ||
* will simply return the cached value. Pending tests. | ||
* Please report bugs. | ||
* will simply return the cached value. | ||
* | ||
* Pending tests. Please report bugs. | ||
* | ||
* @example | ||
@@ -18,11 +19,14 @@ * | ||
export declare function cache<T extends ICallable>(func: T): T; | ||
declare function identityHash(object: any): any; | ||
/** | ||
* Caches the return value of calling a function. using the arguments as keys. | ||
* This is also called memoization. Please don't abuse this function to avoid | ||
* memory inefficiencies. Pending tests. Please report bugs. | ||
* This is also called memoization. | ||
* | ||
* Pending tests. Please report bugs. | ||
* | ||
* @example | ||
* | ||
*/ | ||
export declare function cacheWith<T extends ICallable>(func: T): T; | ||
export declare function cacheWith<T extends ICallable>(func: T, hash?: typeof identityHash): T; | ||
export {}; | ||
//# sourceMappingURL=cache.d.ts.map |
@@ -9,5 +9,6 @@ /** | ||
* Caches the return value of calling a function. Repeat calls | ||
* will simply return the cached value. Pending tests. | ||
* Please report bugs. | ||
* will simply return the cached value. | ||
* | ||
* Pending tests. Please report bugs. | ||
* | ||
* @example | ||
@@ -18,36 +19,37 @@ * | ||
let result, called = false; | ||
return Object.assign((...args) => { | ||
return Object.assign(function (...args) { | ||
if (called) | ||
return result; | ||
result = func(...args); | ||
result = func.call(this, ...args); | ||
called = true; | ||
}, { reset: () => { called = false; } }); | ||
} | ||
const hashes = new WeakMap(); | ||
function identityHash(object) { | ||
if (hashes.has(object)) | ||
return hashes.get(object); | ||
else { | ||
const randomHash = `${Math.random()}`; | ||
hashes.set(object, randomHash); | ||
return randomHash; | ||
} | ||
} | ||
/** | ||
* Caches the return value of calling a function. using the arguments as keys. | ||
* This is also called memoization. Please don't abuse this function to avoid | ||
* memory inefficiencies. Pending tests. Please report bugs. | ||
* This is also called memoization. | ||
* | ||
* Pending tests. Please report bugs. | ||
* | ||
* @example | ||
* | ||
*/ | ||
export function cacheWith(func) { | ||
let results = []; | ||
return Object.assign((...args) => { | ||
let count, i, arg; | ||
for (let result of results) { | ||
count = 0; | ||
for ([i, arg] of args.entries()) { | ||
if (result[i] === arg) | ||
count++; | ||
else | ||
break; | ||
} | ||
if (count === args.length) | ||
return result[i + 1]; | ||
export function cacheWith(func, hash = identityHash) { | ||
let results = {}; | ||
return Object.assign(function (...args) { | ||
const argsHash = args.map(arg => hash(arg)).join('-|-'); | ||
if (!results.hasOwnProperty(argsHash)) { | ||
results[argsHash] = func.call(this, ...args); | ||
} | ||
const result = func(...args); | ||
results.push([...args, result]); | ||
return result; | ||
}, { reset: () => { results = []; } }); | ||
return results[argsHash]; | ||
}, { reset: () => { results = {}; } }); | ||
} |
{ | ||
"name": "deleight", | ||
"version": "5.5.9", | ||
"version": "5.5.10", | ||
"description": "A library with 9 modules for writing more expressive web applications with traditional HTML, CSS and JavaScript.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
581498
0.02%16535
0.07%