This project provides a memoize function for improving performance in JavaScript or TypeScript projects by caching the results of expensive function calls. By memoizing, repeated calls with the same arguments will return the cached result, speeding up execution.
This module works like react's useMemo hook but NOT required react. You can use any framework or pure javascript projects
Npm Package
Github
In the following process, when the concatPhoneNumber method is called again with the same parameters, the function is not executed again, it fetches the result from the cache.
import memofy from "memofy"; const concatPhoneNumber = (extension, number) => { // Heavy calculation // return result }; const memoizedConcatPhoneNumber = memofy(concatPhoneNumber, []); memoizedConcatPhoneNumber(90, 555); // Runs concatPhoneNumber when first run memoizedConcatPhoneNumber(90, 555); // get value from cache memoizedConcatPhoneNumber(90, 552); // Runs concatPhoneNumber because params is change
If you want the method to run again with the same parameter according to some dependencies, you can pass the deps parameter as follows.
import memofy from "memofy"; const taxRatio = 0.5; const product = { title: "Test product", price: 10 }; const calculateTax = () => { // Calculate tax by product price // Heavy calculation return taxRatio * product.price; }; const memoizedConcatPhoneNumber = memofy(calculateTax, [product, taxRatio]); calculatedPrice = calculateTax(); // Runs concatPhoneNumber when first run product.price = 40; let calculatedPrice = calculateTax(); // Runs concatPhoneNumber because product dep changed taxRatio = 0.8; calculatedPrice = calculateTax(); // Runs concatPhoneNumber because taxRatio changed
Performance results on a complex function that distinguishes prime numbers. Performance Test
Case | ms |
---|---|
First execute time (no caching) | > 52.08 ms |
Second execute time (caching) | < 0.03 ms |
and subsequent execution (caching) | < 0.03 ms |
Tests were written for all cases and all parameter types. Tests
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---|---|---|---|---|---|
All files | 100 | 100 | 100 | 100 | 0 |
lib | 100 | 100 | 100 | 100 | 0 |
index.ts | 100 | 100 | 100 | 100 | 0 |
lib/store | 100 | 100 | 100 | 100 | 0 |
CacheStore.ts | 100 | 100 | 100 | 100 | 0 |
DepsStore.ts | 100 | 100 | 100 | 100 | 0 |
The above is the detailed content of Prevents re-execution of large javascript functions that have been processed once with the same parameter.. For more information, please follow other related articles on the PHP Chinese website!