Categorized | Web 2.0

One-Line JavaScript Memoization

Oliver Steele has written up some of his implementations of memoization in JavaScript.

He ends up with:

JAVASCRIPT:
  1.  
  2. function memoizeConstantMethod(o, p) {
  3.   var f = o[p], mf;
  4.   var s = function(v) {return o[p]=v||mf};
  5.   ((mf = function() {
  6.     (s(function(){return value})).reset = mf.reset;
  7.     return value = f.call(this);
  8.   }).reset = s)();
  9. }
  10.  

If you have calculations that you would like to cache, memoization may be a good choice for you.

2 Responses to “One-Line JavaScript Memoization”

Trackbacks/Pingbacks

  1. Make Money says:

    … [Trackback]…

    [...] Read More Infos here: utropicmedia.net/blog/one-line-javascript-memoization [...]…

  2. {Webdesigner says:

    … [Trackback]…

    [...] Read More here: utropicmedia.net/blog/one-line-javascript-memoization [...]…


Leave a Reply