Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
The 'optimism' npm package is a library for memoizing asynchronous functions. It helps in caching the results of function calls and reusing them when the same inputs occur, thus improving performance by avoiding redundant computations.
Memoizing Asynchronous Functions
This feature allows you to memoize asynchronous functions. The 'wrap' function from 'optimism' is used to wrap an async function, caching its results based on the input arguments. Subsequent calls with the same arguments will return the cached result instead of executing the function again.
const { wrap } = require('optimism');
const fetchData = wrap(async (url) => {
const response = await fetch(url);
return response.json();
});
// Usage
fetchData('https://api.example.com/data').then(data => console.log(data));
fetchData('https://api.example.com/data').then(data => console.log(data)); // This call will use the cached result
Custom Cache Key
This feature allows you to define a custom cache key for the memoized function. By providing a 'makeCacheKey' function, you can control how the cache key is generated based on the function's arguments.
const { wrap } = require('optimism');
const fetchData = wrap(async (url, params) => {
const response = await fetch(url, { params });
return response.json();
}, {
makeCacheKey: (url, params) => `${url}-${JSON.stringify(params)}`
});
// Usage
fetchData('https://api.example.com/data', { id: 1 }).then(data => console.log(data));
fetchData('https://api.example.com/data', { id: 1 }).then(data => console.log(data)); // This call will use the cached result
Cache Expiration
This feature allows you to set an expiration time for the cache. By specifying the 'maxAge' option, you can control how long the cached result is valid. After the specified time, the cache will expire, and the function will be executed again to fetch new data.
const { wrap } = require('optimism');
const fetchData = wrap(async (url) => {
const response = await fetch(url);
return response.json();
}, {
maxAge: 60000 // Cache expires after 60 seconds
});
// Usage
fetchData('https://api.example.com/data').then(data => console.log(data));
setTimeout(() => {
fetchData('https://api.example.com/data').then(data => console.log(data)); // This call will fetch new data after cache expiration
}, 61000);
The 'memoizee' package provides a comprehensive solution for memoizing both synchronous and asynchronous functions. It offers more configuration options compared to 'optimism', such as cache size limits, primitive and deep equality checks, and more.
The 'lru-cache' package is a simple and efficient Least Recently Used (LRU) cache implementation. While it does not specifically target memoizing functions, it can be used to cache any kind of data, including function results. It is highly configurable and performant.
The 'async-memoize' package is designed specifically for memoizing asynchronous functions. It provides a straightforward API for caching async function results and supports custom cache keys and expiration times, similar to 'optimism'.
FAQs
Composable reactive caching with efficient invalidation.
The npm package optimism receives a total of 3,353,406 weekly downloads. As such, optimism popularity was classified as popular.
We found that optimism demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.