Skip to content

Commit

Permalink
Underscore 1.0.4, with _.memoize
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Jun 22, 2010
1 parent d629066 commit 29e2c83
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 29 deletions.
52 changes: 36 additions & 16 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,16 @@ <h1>Underscore.js</h1>

<h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and use "Save As")</i></h2>

<p>
<table>
<tr>
<td><a href="underscore.js">Development Version (1.0.3)</a></td>
<td><i>24kb, Uncompressed with Comments</i></td>
</tr>
<tr>
<td><a href="underscore-min.js">Production Version (1.0.3)</a></td>
<td><i>2.9kb, Packed and Gzipped</i></td>
</tr>
</table>
</p>
<table>
<tr>
<td><a href="underscore.js">Development Version (1.0.4)</a></td>
<td><i>24kb, Uncompressed with Comments</i></td>
</tr>
<tr>
<td><a href="underscore-min.js">Production Version (1.0.4)</a></td>
<td><i>2.9kb, Packed and Gzipped</i></td>
</tr>
</table>

<h2>Table of Contents</h2>

Expand All @@ -144,15 +142,16 @@ <h2>Table of Contents</h2>
<br />
<span class="methods"><a href="#first">first</a>, <a href="#rest">rest</a>, <a href="#last">last</a>,
<a href="#compact">compact</a>, <a href="#flatten">flatten</a>, <a href="#without">without</a>, <a href="#uniq">uniq</a>,
<a href="#intersect">intersect</a>, <a href="#zip">zip</a>, <a href="#indexOf">indexOf</a></span>,
<a href="#intersect">intersect</a>, <a href="#zip">zip</a>, <a href="#indexOf">indexOf</a>,
<a href="#lastIndexOf">lastIndexOf</a>, <a href="#range">range</a></span>
</p>

<p>
<b>Functions</b>
<br />
<span class="methods"><a href="#bind">bind</a>, <a href="#bindAll">bindAll</a>, <a href="#delay">delay</a>,
<a href="#defer">defer</a>, <a href="#wrap">wrap</a></span>, <a href="#compose">compose</a></span>
<span class="methods"><a href="#bind">bind</a>, <a href="#bindAll">bindAll</a>,
<a href="#memoize">memoize</a>, <a href="#delay">delay</a>, <a href="#defer">defer</a>,
<a href="#wrap">wrap</a>, <a href="#compose">compose</a></span>
</p>

<p>
Expand All @@ -173,7 +172,7 @@ <h2>Table of Contents</h2>
<br />
<span class="methods"><a href="#noConflict">noConflict</a>,
<a href="#identity">identity</a>, <a href="#times">times</a>,
<a href="#breakLoop">breakLoop</a></span>, <a href="#mixin">mixin</a></span>,
<a href="#breakLoop">breakLoop</a>, <a href="#mixin">mixin</a>,
<a href="#uniqueId">uniqueId</a>, <a href="#template">template</a></span>
</p>

Expand Down Expand Up @@ -652,6 +651,21 @@ <h2>Function (uh, ahem) Functions</h2>
_.bindAll(buttonView);
jQuery('#underscore_button').bind('click', buttonView.onClick);
=&gt; When the button is clicked, this.label will have the correct value...
</pre>

<p id="memoize">
<b class="header">memoize</b><code>_.memoize(function, [hashFunction])</code>
<br />
Memoizes a given <b>function</b> by caching the computed result. Useful
for speeding up slow-running computations. If passed an optional
<b>hashFunction</b>, it will be used to compute the hash key for storing
the result, based on the arguments to the original function.
</p>
<pre>
var fibonacci = function(n) {
return n &lt; 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
};
var fastFibonacci = _.memoize(fibonacci);
</pre>

<p id="delay">
Expand Down Expand Up @@ -1136,6 +1150,12 @@ <h2>Links &amp; Suggested Reading</h2>

<h2>Change Log</h2>

<p>
<b class="header">1.0.4</b><br />
Andri Möll contributed the <tt>_.memoize</tt> function, which can be
used to speed up expensive repeated computations by caching the results.
</p>

<p>
<b class="header">1.0.3</b><br />
Patch that makes <tt>_.isEqual</tt> return <tt>false</tt> if any property
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"dependencies" : [],
"lib" : ".",
"main" : "underscore.js",
"version" : "1.0.3"
"version" : "1.0.4"
}
9 changes: 9 additions & 0 deletions test/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ $(document).ready(function() {
equals(curly.sayHi(), 'hi: moe', 'calling bindAll with no arguments binds all functions to the object');
});

test("functions: memoize", function() {
var fib = function(n) {
return n < 2 ? n : fib(n - 1) + fib(n - 2);
};
var fastFib = _.memoize(fib);
equals(fib(10), 55, 'a memoized version of fibonacci produces identical results');
equals(fastFib(10), 55, 'a memoized version of fibonacci produces identical results');
});

asyncTest("functions: delay", function() {
var delayed = false;
_.delay(function(){ delayed = true; }, 100);
Expand Down
2 changes: 1 addition & 1 deletion test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $(document).ready(function() {
"flatten", "foldl", "foldr", "forEach", "functions", "head", "identity", "include",
"indexOf", "inject", "intersect", "invoke", "isArguments", "isArray", "isBoolean", "isDate", "isElement", "isEmpty", "isEqual",
"isFunction", "isNaN", "isNull", "isNumber", "isRegExp", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max",
"methods", "min", "mixin", "noConflict", "pluck", "range", "reduce", "reduceRight", "reject", "rest", "select",
"memoize", "methods", "min", "mixin", "noConflict", "pluck", "range", "reduce", "reduceRight", "reject", "rest", "select",
"size", "some", "sortBy", "sortedIndex", "tail", "tap", "template", "times", "toArray", "uniq",
"uniqueId", "values", "without", "wrap", "zip"];
same(expected, _.methods(_), 'provides a sorted list of functions');
Expand Down
20 changes: 10 additions & 10 deletions underscore-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
root._ = _;

// Current version.
_.VERSION = '1.0.3';
_.VERSION = '1.0.4';

// ------------------------ Collection Functions: ---------------------------

Expand Down Expand Up @@ -375,6 +375,16 @@
return obj;
};

// Memoize an expensive function by storing its results.
_.memoize = function(func, hasher) {
var memo = {};
hasher = hasher || _.identity;
return function() {
var key = hasher.apply(this, arguments);
return key in memo ? memo[key] : (memo[key] = func.apply(this, arguments));
};
};

// Delays a function for the given number of milliseconds, and then calls
// it with the arguments supplied.
_.delay = function(func, wait) {
Expand Down

0 comments on commit 29e2c83

Please sign in to comment.