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

memoize-one

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

memoize-one

A memoization library which only remembers the latest invocation

  • 5.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12M
increased by4.8%
Maintainers
1
Weekly downloads
 
Created

What is memoize-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.

What are memoize-one's main functionalities?

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);

Other packages similar to memoize-one

Keywords

FAQs

Package last updated on 08 Apr 2019

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