GithubHelp home page GithubHelp logo

flavorus / lru-memoize Goto Github PK

View Code? Open in Web Editor NEW

This project forked from erikras/lru-memoize

0.0 4.0 0.0 18 KB

A utility to provide LRU memoization for any js function

License: MIT License

JavaScript 100.00%

lru-memoize's Introduction

#lru-memoize

NPM Version NPM Downloads Build Status

lru-memoize is a utility to provide simple memoization for any pure javascript function, using an LRU cache that prioritizes the most recently accessed values, and discards the "least recently used" (LRU) items when the size limit is reached. If your function has side effects or relies on some external state to generate its result, it should not be memoized.

Installation

npm install --save lru-memoize

Usage

Let's look at an example where we want to memoize a function that multiplies three numbers together, and we want to keep the last ten arguments -> value mappings in memory.

ES5

var memoize = require('lru-memoize');

var multiply = function(a, b, c) {
  return a * b * c;
}

multiply = memoize(10)(multiply);

module.exports = multiply;

ES6

import memoize from 'lru-memoize';

let multiply = (a, b, c) => a * b * c;

multiply = memoize(10)(multiply);

export default multiply;

API

memoize(limit:Integer?, equals:Function?, deepObjects:Boolean?)

Returns (Function) => Function.

-limit : Integer [optional]

The number of arguments -> value mappings to keep in memory. Defaults to 1.

-equals : Function [optional]

A function to compare two values for equality. Defaults to ===.

-deepObjects : Boolean [optional]

Whether or not to perform a deep equals on Object values. Defaults to false.

TypeScript

Type definitions can be found in index.d.ts which should automatically be picked up by the TypeScript compiler when this package is installed.

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.