Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A JavaScript library for memoizing the result of a pure function

License

NotificationsYou must be signed in to change notification settings

bigcommerce/memoize-js

CircleCI

This library can be used to memoize the result of a pure function.

Unlike the defaultmemoize function provided by Lodash, it can be applied to functions that accept multiple non-primitive arguments. It can also be configured to expire its cache after certain number of unique calls. By default, it compares object-based arguments shallowly; but it can be configured to compare arguments strictly or deeply depending on your usage requirement.

Install

You can install this library usingnpm.

npm install --save @bigcommerce/memoize

Usage

To memoize a function:

functionfn(a,b){return{ a, b};}constmemoizedFn=memoize(fn);constresult=memoizedFn({message:'hello'},{message:'world'});constresult2=memoizedFn({message:'hello'},{message:'world'});expect(result).toBe(result2);

To set a limit on the cache size:

functionfn(a,b){return{ a, b};}constmemoizedFn=memoize(fn,{maxSize:1});constresult=memoizedFn({message:'hello'},{message:'world'});// This call will expire the cache of the previous call because it is called with a different set of argumentsconstresult2=memoizedFn({message:'hello'},{message:'foobar'});constresult3=memoizedFn({message:'hello'},{message:'world'});expect(result3).not.toBe(result);

There is a convenience method for setting the cache size to one:

constmemoizedFn=memoizeOne(fn);

To use a different argument comparison function:

constmemoizedFn=memoize(fn,{isEqual:(a,b)=>a===b,});

Contribution

To release:

npm run release

To see other available commands:

npm run

License

MIT

About

A JavaScript library for memoizing the result of a pure function

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors5


[8]ページ先頭

©2009-2025 Movatter.jp