Prevents re-execution of large javascript functions that have been processed once with the same parameter.

memofy

Cache mechanism(memoizer) for functions executed with the same parameters (Only 1.14 KB)

This project provides a memoize function for improving performance in JavaScript or TypeScript projects by caching the results of expe…


This content originally appeared on DEV Community and was authored by Ahmet İlhan

memofy

Cache mechanism(memoizer) for functions executed with the same parameters (Only 1.14 KB)

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

Features

  • Function Memoization: Caches results of function calls with the same arguments.
  • Dependency Tracking: Updates the cache if dependencies change.
  • Flexibility: Usable in both JavaScript and TypeScript projects.
  • The best solution for CPU-intensive operations or complex calculations
  • The disconnected functions are deleted from memory. The caches belonging to this function are also deleted.
  • WeakMap based cache store
  • WeakMap Disconnects methods that cannot communicate with weak reference links and triggers the garbage collector to kick in

Usage case

Without deps parameters

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

With deps parameter

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 result

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

Test coverage result

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


This content originally appeared on DEV Community and was authored by Ahmet İlhan


Print Share Comment Cite Upload Translate Updates
APA

Ahmet İlhan | Sciencx (2024-08-27T18:39:17+00:00) Prevents re-execution of large javascript functions that have been processed once with the same parameter.. Retrieved from https://www.scien.cx/2024/08/27/prevents-re-execution-of-large-javascript-functions-that-have-been-processed-once-with-the-same-parameter/

MLA
" » Prevents re-execution of large javascript functions that have been processed once with the same parameter.." Ahmet İlhan | Sciencx - Tuesday August 27, 2024, https://www.scien.cx/2024/08/27/prevents-re-execution-of-large-javascript-functions-that-have-been-processed-once-with-the-same-parameter/
HARVARD
Ahmet İlhan | Sciencx Tuesday August 27, 2024 » Prevents re-execution of large javascript functions that have been processed once with the same parameter.., viewed ,<https://www.scien.cx/2024/08/27/prevents-re-execution-of-large-javascript-functions-that-have-been-processed-once-with-the-same-parameter/>
VANCOUVER
Ahmet İlhan | Sciencx - » Prevents re-execution of large javascript functions that have been processed once with the same parameter.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/27/prevents-re-execution-of-large-javascript-functions-that-have-been-processed-once-with-the-same-parameter/
CHICAGO
" » Prevents re-execution of large javascript functions that have been processed once with the same parameter.." Ahmet İlhan | Sciencx - Accessed . https://www.scien.cx/2024/08/27/prevents-re-execution-of-large-javascript-functions-that-have-been-processed-once-with-the-same-parameter/
IEEE
" » Prevents re-execution of large javascript functions that have been processed once with the same parameter.." Ahmet İlhan | Sciencx [Online]. Available: https://www.scien.cx/2024/08/27/prevents-re-execution-of-large-javascript-functions-that-have-been-processed-once-with-the-same-parameter/. [Accessed: ]
rf:citation
» Prevents re-execution of large javascript functions that have been processed once with the same parameter. | Ahmet İlhan | Sciencx | https://www.scien.cx/2024/08/27/prevents-re-execution-of-large-javascript-functions-that-have-been-processed-once-with-the-same-parameter/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.