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

p-memoize

Package Overview
Dependencies
Maintainers
2
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-memoize

Memoize promise-returning & async functions

  • 7.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.1M
increased by12.03%
Maintainers
2
Weekly downloads
 
Created

What is p-memoize?

The p-memoize package is a promise-memoization library for JavaScript. It allows you to memoize the results of asynchronous functions, which can help improve performance by caching the results of expensive function calls and returning the cached result when the same inputs occur again.

What are p-memoize's main functionalities?

Basic Memoization

This feature allows you to memoize an asynchronous function. The first call to the function with a specific input will take the usual time, but subsequent calls with the same input will return the cached result immediately.

const pMemoize = require('p-memoize');

const expensiveFunction = async (input) => {
  // Simulate an expensive operation
  return new Promise(resolve => setTimeout(() => resolve(input * 2), 1000));
};

const memoizedFunction = pMemoize(expensiveFunction);

(async () => {
  console.log(await memoizedFunction(2)); // Waits 1 second, logs 4
  console.log(await memoizedFunction(2)); // Logs 4 immediately
})();

Custom Cache

This feature allows you to use a custom cache implementation. In this example, QuickLRU is used as the cache, which provides a least-recently-used (LRU) cache mechanism.

const pMemoize = require('p-memoize');
const QuickLRU = require('quick-lru');

const expensiveFunction = async (input) => {
  // Simulate an expensive operation
  return new Promise(resolve => setTimeout(() => resolve(input * 2), 1000));
};

const cache = new QuickLRU({ maxSize: 1000 });
const memoizedFunction = pMemoize(expensiveFunction, { cache });

(async () => {
  console.log(await memoizedFunction(2)); // Waits 1 second, logs 4
  console.log(await memoizedFunction(2)); // Logs 4 immediately
})();

Cache Key Customization

This feature allows you to customize the cache key. By default, the cache key is generated based on the function arguments, but you can provide a custom function to generate the cache key.

const pMemoize = require('p-memoize');

const expensiveFunction = async (input) => {
  // Simulate an expensive operation
  return new Promise(resolve => setTimeout(() => resolve(input * 2), 1000));
};

const memoizedFunction = pMemoize(expensiveFunction, {
  cacheKey: ([input]) => `key-${input}`
});

(async () => {
  console.log(await memoizedFunction(2)); // Waits 1 second, logs 4
  console.log(await memoizedFunction(2)); // Logs 4 immediately
})();

Other packages similar to p-memoize

Keywords

FAQs

Package last updated on 07 Oct 2022

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