Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
memoize-one
Advanced tools
A memoization library for memoizing a function with a cache size of one
The memoize-one package is a simple and lightweight memoization library designed for memoizing the result of a function based on the latest arguments. It only remembers the latest arguments and result, and it will only recompute the result when the arguments change. This can be particularly useful for optimizing performance in scenarios where expensive function calls are frequently made with the same arguments.
Simple memoization of functions
This feature allows you to create a memoized version of a function that caches the result based on the latest set of arguments it was called with. If the function is called again with the same arguments, the cached result is returned instead of recomputing.
const memoizeOne = require('memoize-one');
const add = (a, b) => a + b;
const memoizedAdd = memoizeOne(add);
console.log(memoizedAdd(1, 2)); // 3
console.log(memoizedAdd(1, 2)); // 3, cached result
console.log(memoizedAdd(2, 2)); // 4, recomputed because arguments changed
Custom equality function
This feature allows you to provide a custom function to compare the equality of arguments. This is useful when you need to memoize a function that takes complex arguments like objects or arrays and the default shallow comparison is not sufficient.
const memoizeOne = require('memoize-one');
const isEqual = (newArgs, lastArgs) => JSON.stringify(newArgs) === JSON.stringify(lastArgs);
const complexFunction = (obj) => {/* complex operation */};
const memoizedComplexFunction = memoizeOne(complexFunction, isEqual);
Lodash provides a memoize function that can cache the result of function calls based on the arguments passed. It allows for custom cache implementations and is part of the larger Lodash utility library, which provides a wide range of functions for manipulating and traversing data.
Fast-memoize is a high-performance memoization library that claims to be the fastest possible memoization library in JavaScript. It supports multiple argument memoization and provides various options for cache creation, argument serialization, and strategy selection.
Reselect is a selector library for Redux that uses memoization to efficiently compute derived data from the Redux store. It is specifically designed for use with Redux and allows for creating memoized selector functions that can compute derived data, optimizing performance for Redux applications.
A simple memoization library which only remembers the latest invokation
Cache invalidation is hard:
There are only two hard things in Computer Science: cache invalidation and naming things.
Phil Karlton
So keep things simple and just use a cache size of one.
Unlike other memoization libraries, memoizeOne
only remembers the latest arguments. No need to worry about cache busting mechanisms such as maxAge
, maxSize
, exlusions
and so on which can be prone to memory leaks. memoizeOne
simply remembers the last arguments, and if the function is next called with the same arguments then it returns the previous result.
import memoizeOne from 'memoize-one';
const add = (a, b) => a + b;
const memoizedAdd = memoizeOne(add);
memoizedAdd(1, 2); // 3
memoizedAdd(1, 2); // 3
// Add function is not executed: previous result is returned
memoizedAdd(2, 3); // 5
// Add function is called to get new value
memoizedAdd(2, 3); // 5
// Add function is not executed: previous result is returned
memoizedAdd(1, 2); // 3
// Add function is called to get new value.
// While this was previously cached,
// it is not the latest so the cached result is lost
You can also pass in a custom function for checking the equality of two items.
import memoizeOne from 'memoize-one';
import deepEqual from 'lodash.isEqual';
const identity = x => x;
const defaultMemoization = memoizeOne(identity);
const customMemoization = memoizeOne(identity, deepEqual);
const result1 = defaultMemoization({foo: 'bar'});
const result2 = defaultMemoization({foo: 'bar'});
result1 === result2 // false - difference reference
const result3 = customMemoization({foo: 'bar'});
const result4 = customMemoization({foo: 'bar'});
result3 === result4 // true - arguments are deep equal
# yarn
yarn add memoize-one
# npm
npm install memoize-one --save
this
bindingFAQs
A memoization library which only remembers the latest invocation
The npm package memoize-one receives a total of 8,732,990 weekly downloads. As such, memoize-one popularity was classified as popular.
We found that memoize-one demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.