Underscore 1.0.4, with _.memoize

This commit is contained in:
Jeremy Ashkenas
2010-06-22 09:21:03 -04:00
parent d62906672b
commit 29e2c832bc
6 changed files with 68 additions and 29 deletions

View File

@@ -55,7 +55,7 @@
root._ = _;
// Current version.
_.VERSION = '1.0.3';
_.VERSION = '1.0.4';
// ------------------------ Collection Functions: ---------------------------
@@ -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) {