Giter Club home page Giter Club logo

memoize.js's Introduction

Memoize.js

A faster JavaScript memoizer

Whilst not new by any means, memoization is a useful optimization technique for caching the results of function calls such that lengthy lookups or expensive recursive computations can be minimized where possible.

The basic idea is that if you can detect an operation has already been previously completed for a specific set of input values, the stored result can instead be returned instead of repeating the operation again.

Some of the problems memoization can help optimize include: recursive mathematics, algorithmic caching of canvas animation and more generally, any problems which can be expressed as a set of calls to the same function with a combination of argument values that repeat.

Usage

Imagine having a function you want to memoize, like running the Fibbonacci sequence:

var fib, fiborg;

fiborg = fib = function (x) {
  if(x < 2) return 1; else return fib(x-1) + fib(x-2);
}

We can then memoize it as follows:

fib = memoize(fiborg);
fib(20);

License

Released under an MIT license.

memoize.js's People

Contributors

addyosmani avatar jamiemason avatar jiahut avatar lagden avatar alessioalex avatar evanhahn avatar

Stargazers

 avatar  avatar Yasin ATEŞ avatar Richard Terungwa Kombol avatar  avatar void avatar LiuHui avatar Ksenia Platova avatar Ashwin M avatar Denis Sebeldin avatar ZhangBoXuan avatar juju avatar Raphael Guastaferro avatar Luan AP avatar  avatar Atakan Goktepe avatar Phellipe Andrade avatar Jason R Alexander avatar Corentin Leruth avatar Hubert Jagodziński avatar cgons avatar Alexey avatar Zhaoxin Zhang avatar NLTESOWN avatar Serhii Zelinskyi avatar Martin "Marrow" Rowlinson avatar  avatar Zakhar Gulchak avatar Stone Sun avatar  avatar Niels Dequeker avatar Mert Kahyaoğlu avatar Gaus Surahman avatar Javier López de Ancos avatar Jeremy Wandui avatar Sergey Sharov avatar Hsun avatar Moritz Kneilmann avatar Jordan avatar  avatar Gordon Zhu avatar foo avatar NoScripter avatar Irina Sokolovskaia avatar Alexandr avatar Nick Esquerra avatar yurik256 avatar Andre Rosot avatar AJ avatar Ross Lemenille avatar Angus H. avatar Rozario Chivers avatar Juan Riquelme avatar  avatar Nazim Amin avatar Quincy Acklen avatar Tijn Kersjes avatar Ma Yuewen avatar rouzbeh84 avatar danigb avatar Pedro Araujo avatar Felix avatar Alesei N avatar Michael Anthony avatar Leif Olsen avatar tom speak avatar Victor Saiz avatar Colby Rogness avatar Sergey Todyshev avatar Gergő Sulymosi avatar Dmytro Voronianski avatar George Raptis avatar Matthew avatar nmccready avatar Gabriel Scindian avatar Aswini S avatar Daniel Eisenbarger avatar Ger Hobbelt avatar Terry Cai avatar SeukWon Kang avatar Dillon de Voor avatar Jordan Leigh avatar  avatar Nikita Semenistyi avatar Dave Poon avatar  avatar Adam Beres-Deak avatar jdahe avatar  avatar  avatar Daniel Husar avatar James B. Pollack, MFA  avatar Chris Webb avatar Thiago Dourado de Andrade avatar JP McGarrity avatar Maksymilian Barnaś avatar Nathan Brock avatar Peter Hodgkinson avatar Simon Belluzzo avatar  avatar

Watchers

Dmitry Baranovskiy avatar Rozario Chivers avatar  avatar  avatar  avatar James Cloos avatar Terry Cai avatar Michael Anthony avatar Ashwin asokan avatar Gabriel Scindian avatar Andre Rosot avatar  avatar

memoize.js's Issues

Add try/catch around JSON.stringify?

As @DmitryBaranovskiy commented;
"Should we add try/catch around JSON.stringify? There is a case when somebody passes cyclic structure to the function. It could not be able to cache it, but at least it should return the value."

var a = { b: null },
    b = { a: a };

a.b = b;

JSON.stringify(a); // >> TypeError: circular_structure

Trade-off is working code in 100% of cases vs performant code for majority of cases, with some broken edge cases.

Resolve issues for Underscore.js pull

I've taken the work we've done so far and put it into a pull request for inclusion in Underscore.js as a viable replacement to their _.memoize() method: jashkenas/underscore#330 (all tests pass).

Performance tests comparing the most recent version of memoize.js to underscore's implem are here from today: http://jsperf.com/comparison-of-memoization-implementations/12

If you take a look at the comments from Jeremy, his main concerns at the moment are that we've dropped the second argument in the memoize signature they use (the hasher function). He's also concerned about the dependency on the JSON object being present, and our reliance on it for stringification.

We do have an open issue attempting to solve this with an alternative, but as you might know, a reliable stringification method needs to cover lots of use cases and can't (as far as I know) be distilled down to something which they might consider including. With this in mind, I've got some questions:

  1. Do you guys think we should pursue trying to get this into Underscore?.
  2. What are your thoughts on a replacement to the JSON stringification we're doing at the moment? (I would look at the comments from Jeremy on the pull).
  3. If we do factor in an optional hasher function, do you think we should apply this to just the pull request or this repo too?. I mentioned in the request, but I haven't really seen too many people need the second argument.

//cc @JamieMason @DmitryBaranovskiy

Cheers!
Addy

Multiple args aren't memoized properly

This is broken:

function test(a, b) {
  return a * 10 + b;
}

test(11, 1); // 111
test(1, 11); // 21

var memo = memoize(test);
memo(11, 1); // 111
memo(1, 11); // 111

Suggestion: build an array of args-to-be-memoized and then use JSON.stringify to convert that into a cache key. It's still not perfect, but it will reduce the number of edge cases.

Put it on NPM

I would really like to have this module in Node. Thanks!

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.