"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Prevents re-execution of large javascript functions that have been processed once with the same parameter.

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

Published on 2024-08-29
Browse:693

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 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)
and subsequent execution (caching)

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
Release Statement This article is reproduced at: https://dev.to/ahmetilhn/prevents-re-execution-of-large-javascript-functions-that-have-been-processed-once-with-the-same-parameter-1ici?1 as If there is any infringement, please contact [email protected] to delete it.
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3