该项目提供了 memoize 函数,通过缓存昂贵的函数调用结果来提高 JavaScript 或 TypeScript 项目的性能。通过记忆,使用相同参数重复调用将返回缓存的结果,从而加快执行速度。
这个模块的工作原理类似于 React 的 useMemo hook,但不需要 React。您可以使用任何框架或纯 javascript 项目
使用 Memofy,您可以将函数的执行时间减少多达 1500 倍。以下结果是通过重函数测试得到的。 ??
Test Case | Function Execute Time (ms) |
---|---|
With Memofy (CACHED) | 0.083 ms |
Without Memofy | 57.571 ms |
没有 Memofy
import memofy from "memofy"; const dep1 = /** Variable to track change */ const heavyMethod = memofy((arg1, arg2, ...args) => { // calculate something then return }, [dep1, dep2, ...deps]); // heavyMethod looks at the deps and arguments every time it is called. // If there is no change, it brings it from the cache. If there is a change, it runs the function again
npm install memofy
yarn add memofy
没有 deps 参数
import memofy from "memofy"; const concatPhoneNumber = (extension, number) => { // Heavy calculation // return result }; const memoizedConcatPhoneNumber = memofy(concatPhoneNumber, []); memoizedConcatPhoneNumber(90, 555); // Runs concatPhoneNumber once memoizedConcatPhoneNumber(90, 555); // Don't run because fetched from cache (same parameter) memoizedConcatPhoneNumber(90, 552); // Runs concatPhoneNumber because params is changed
带 deps 参数
import memofy from "memofy"; const product = { title: "Test product", price: 10 }; const calculateTax = (taxRatio) => { // Calculate tax by product price // Heavy calculation return taxRatio * product.price; }; const memoizedCalculateTax = memofy(calculateTax, [product]); calculateTax(2); // Runs calculateTax when first run -> 20 calculateTax(2); // // Don't run because fetched from cache (same parameter and same deps) -> 20 product.price = 40; calculateTax(3); // Runs calculateTax because product dep changed -> 120
import memofy from "memofy"; const products = [ /**Let's say there are more than 100 products */ ]; // It is costly to cycle through 100 products each time. Just keep the result in the cache when it runs once. const getTotalPrice = (fixPrice) => { return products.reduce((acc, curr) => acc + curr.price, 0); }; const _getTotalPrice = memofy(getTotalPrice, [products]); getTotalPrice(0); // Runs getTotalPrice once getTotalPrice(0); // Don't run because fetched from cache products.push({ /** a few products */ }); getTotalPrice(2); // Runs again getTotalPrice because products and parameter changed
有上下文
import memofy from "memofy"; this.user.name = "Jack"; // For example inject name to context const getName = (suffix) => { return `${suffix} ${this.user.name}`; }; const memoizedGetName = memofy(getName, [], this); memoizedGetName("Mr"); // Result is Mr Jack this.user.name = "John"; memoizedGetName("Mr"); // Result is Mr John because context data changed
type Args = Array<any>; type Deps = Readonly<Array<any>>; type MemoizedFunction<A extends Args, ReturnType> = (...args: A) => ReturnType; declare function memofy<A extends Args, ReturnType extends any>( _functionToMemoize: (...args: Array<unknown>) => ReturnType, _deps?: Deps, _context?: unknown ): MemoizedFunction<A, ReturnType>;
绩效结果
Case | ms |
---|---|
First execute time (no caching) | > 52.08 ms |
Second execute time (caching) | < 0.03 ms |
and subsequent execution (caching) | < 0.03 ms |
测试覆盖率结果
File | % Stmts | % Branch | % Funcs | % Lines |
---|---|---|---|---|
All files | 90.69 | 86.95 | 100 | 94.59 |
lib | 88.88 | 92.3 | 100 | 87.5 |
index.ts | 88.88 | 92.3 | 100 | 87.5 |
lib/store | 92 | 80 | 100 | 100 |
DependencyCacheStore.ts.ts | 90 | 75 | 100 | 100 |
FunctionCacheStore.ts | 93.33 | 83.33 | 100 | 100 |
贡献
以上是每个框架的函数存储器的详细内容。更多信息请关注PHP中文网其他相关文章!