0.2.0 • Published 10 months ago

light-memoize v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

light-memoize

light-memoize is a lightweight JavaScript library that provides an easy way to create memoization functions. It allows developers to efficiently cache the results of expensive function calls and retrieve them later based on the input arguments.

Features

  • Automatically memoizes the result of a function call by serializing the arguments into a string key.
  • Provides the option to define a custom memoize key generator based on the input arguments.

Installation

You can install light-memoize via npm by running the following command:

npm install light-memoize

Usage

1. Memoize function without a custom key generator:

const originalFunc = (arg1, arg2) => {
  // Function implementation
};

const memoizeFunc = memoize(originalFunc);

memoizeFunc({ x: 1, y: 1}, 1)
memoizeFunc({ x: 0, y: 0}, 1)
memoizeFunc({ x: 1, y: 1}, 1) // Cache result

In the example above, memoizeFunc is a memoized version of originalFunc. The result of each function call will be cached based on the serialized arguments, and subsequent calls with the same arguments will retrieve the cached result instead of re-executing the function.

2. Memoize function with a custom key generator:

const originalFunc = (arg1, arg2) => {
  // Function implementation
};

const generator = (args) => `${Math.abs(args[0].x)}, ${Math.abs(args[0].y)}`;

const memoizeFunc = memoize(originalFunc, generator);

memoizeFunc({ x: 1, y: 1}, 1)
memoizeFunc({ x: 0, y: 0}, 1)
memoizeFunc({ x: -1, y: -1}, 1) // Cache result

In this example, generator is a custom key generator function that takes an array of input arguments and returns a memoization key. The memoized function will use the key generated by generator to cache and retrieve results.

Contributing

Contributions are welcome! If you have any bug fixes, enhancements, or new features to propose, please open an issue or submit a pull request on the GitHub repository: light-memoize.

Before contributing, please ensure that you have read the Contribution Guidelines.

License

light-memoize is MIT licensed.

0.2.0

10 months ago

0.1.0

10 months ago