GithubHelp home page GithubHelp logo

isabella232 / memoize-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bigcommerce/memoize-js

0.0 0.0 0.0 653 KB

A JavaScript library for memoizing the result of a pure function

License: MIT License

JavaScript 3.00% TypeScript 97.00%

memoize-js's Introduction

@bigcommerce/memoize

CircleCI

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

Unlike the default memoize 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 using npm.

npm install --save @bigcommerce/memoize

Usage

To memoize a function:

function fn(a, b) {
    return { a, b };
}

const memoizedFn = memoize(fn);
const result = memoizedFn({ message: 'hello' }, { message: 'world' });
const result2 = memoizedFn({ message: 'hello' }, { message: 'world' });

expect(result).toBe(result2);

To set a limit on the cache size:

function fn(a, b) {
    return { a, b };
}

const memoizedFn = memoize(fn, { maxSize: 1 });
const result = memoizedFn({ message: 'hello' }, { message: 'world' });

// This call will expire the cache of the previous call because it is called with a different set of arguments
const result2 = memoizedFn({ message: 'hello' }, { message: 'foobar' });
const result3 = memoizedFn({ message: 'hello' }, { message: 'world' });

expect(result3).not.toBe(result);

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

const memoizedFn = memoizeOne(fn);

To use a different argument comparison function:

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

Contribution

To release:

npm run release

To see other available commands:

npm run

License

MIT

memoize-js's People

Contributors

davidchin avatar pascalzajac avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.