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

mem

Package Overview
Dependencies
Maintainers
2
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mem

Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input

  • 10.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.6M
increased by51.18%
Maintainers
2
Weekly downloads
 
Created

What is mem?

The 'mem' npm package is a utility for memoizing functions, which means it caches the result of function calls based on the arguments provided. This can significantly improve performance for expensive or frequently called functions by avoiding redundant computations.

What are mem's main functionalities?

Basic Memoization

This feature allows you to memoize a function so that it caches the result of function calls based on the arguments. Subsequent calls with the same arguments will return the cached result instead of recalculating.

const mem = require('mem');

const expensiveFunction = (input) => {
  console.log('Function called with', input);
  return input * 2;
};

const memoizedFunction = mem(expensiveFunction);

console.log(memoizedFunction(2)); // Function called with 2, 4
console.log(memoizedFunction(2)); // 4 (cached result)

Custom Cache Key

This feature allows you to define a custom cache key function, which determines how the cache key is generated based on the function arguments. This can be useful for more complex caching strategies.

const mem = require('mem');

const expensiveFunction = (input) => {
  console.log('Function called with', input);
  return input * 2;
};

const customCacheKey = (args) => args[0] % 2; // Cache based on even/odd
const memoizedFunction = mem(expensiveFunction, { cacheKey: customCacheKey });

console.log(memoizedFunction(2)); // Function called with 2, 4
console.log(memoizedFunction(4)); // 4 (cached result)
console.log(memoizedFunction(3)); // Function called with 3, 6

Cache Expiration

This feature allows you to set a maximum age for cache entries. After the specified time, the cache entry will expire, and the function will be called again to recalculate the result.

const mem = require('mem');

const expensiveFunction = (input) => {
  console.log('Function called with', input);
  return input * 2;
};

const memoizedFunction = mem(expensiveFunction, { maxAge: 1000 });

console.log(memoizedFunction(2)); // Function called with 2, 4
setTimeout(() => {
  console.log(memoizedFunction(2)); // Function called with 2, 4 (after 1 second, cache expired)
}, 1500);

Other packages similar to mem

Keywords

FAQs

Package last updated on 14 Nov 2023

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