Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lru-memoizer

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lru-memoizer

Memoize functions results using an lru-cache.

  • 3.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.2M
decreased by-0.26%
Maintainers
1
Weekly downloads
 
Created

What is lru-memoizer?

The lru-memoizer npm package is a utility for memoizing function results with an LRU (Least Recently Used) cache. This helps in optimizing performance by storing the results of expensive function calls and reusing them when the same inputs occur again, while also managing memory usage by discarding the least recently used items when the cache limit is reached.

What are lru-memoizer's main functionalities?

Basic Memoization

This feature allows you to memoize a function so that its results are cached. Subsequent calls with the same arguments will return the cached result, improving performance.

const memoizer = require('lru-memoizer');

const slowFunction = (num) => {
  // Simulate a slow computation
  for (let i = 0; i < 1e6; i++);
  return num * 2;
};

const memoizedFunction = memoizer({ load: slowFunction, max: 100 });

console.log(memoizedFunction(5)); // First call, slow
console.log(memoizedFunction(5)); // Second call, fast (cached)

Custom Cache Key

This feature allows you to define a custom cache key for the memoized function. This is useful when the function arguments are complex objects and you want to control how they are hashed for caching.

const memoizer = require('lru-memoizer');

const slowFunction = (obj) => {
  // Simulate a slow computation
  for (let i = 0; i < 1e6; i++);
  return obj.value * 2;
};

const memoizedFunction = memoizer({
  load: slowFunction,
  max: 100,
  hash: (obj) => obj.key // Custom cache key
});

console.log(memoizedFunction({ key: 'a', value: 5 })); // First call, slow
console.log(memoizedFunction({ key: 'a', value: 5 })); // Second call, fast (cached)

Cache Statistics

This feature provides statistics about the cache usage, such as the number of hits, misses, and the current number of keys in the cache. This can be useful for monitoring and debugging.

const memoizer = require('lru-memoizer');

const slowFunction = (num) => {
  // Simulate a slow computation
  for (let i = 0; i < 1e6; i++);
  return num * 2;
};

const memoizedFunction = memoizer({ load: slowFunction, max: 100 });

memoizedFunction(5);
memoizedFunction(5);

console.log(memoizedFunction.cache.stats); // { hits: 1, misses: 1, keys: 1 }

Other packages similar to lru-memoizer

Keywords

FAQs

Package last updated on 14 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc