Speeding up a few methods which assign a default identity function for missing/optional iterators. Noticeable difference.

This commit is contained in:
Samuel Clay
2011-04-05 18:11:20 -04:00
parent c8e3c04076
commit 5457522582

View File

@@ -167,7 +167,7 @@
// Returns a count of elements which pass a truth test. // Returns a count of elements which pass a truth test.
_.count = function(obj, iterator, context) { _.count = function(obj, iterator, context) {
var count = 0; var count = 0;
iterator = iterator || _.identity; iterator || (iterator = _.identity);
each(obj, function(value, index, list) { each(obj, function(value, index, list) {
if (iterator.call(context, value, index, list)) count += 1; if (iterator.call(context, value, index, list)) count += 1;
}); });
@@ -178,7 +178,7 @@
// Delegates to **ECMAScript 5**'s native `every` if available. // Delegates to **ECMAScript 5**'s native `every` if available.
// Aliased as `all`. // Aliased as `all`.
_.every = _.all = function(obj, iterator, context) { _.every = _.all = function(obj, iterator, context) {
iterator = iterator || _.identity; iterator || (iterator = _.identity);
var result = true; var result = true;
if (obj == null) return result; if (obj == null) return result;
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context); if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
@@ -192,7 +192,7 @@
// Delegates to **ECMAScript 5**'s native `some` if available. // Delegates to **ECMAScript 5**'s native `some` if available.
// Aliased as `any`. // Aliased as `any`.
var any = _.some = _.any = function(obj, iterator, context) { var any = _.some = _.any = function(obj, iterator, context) {
iterator = iterator || _.identity; iterator || (iterator = _.identity);
var result = false; var result = false;
if (obj == null) return result; if (obj == null) return result;
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context); if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
@@ -265,7 +265,7 @@
// Use a comparator function to figure out at what index an object should // Use a comparator function to figure out at what index an object should
// be inserted so as to maintain order. Uses binary search. // be inserted so as to maintain order. Uses binary search.
_.sortedIndex = function(array, obj, iterator) { _.sortedIndex = function(array, obj, iterator) {
iterator = iterator || _.identity; iterator || (iterator = _.identity);
var low = 0, high = array.length; var low = 0, high = array.length;
while (low < high) { while (low < high) {
var mid = (low + high) >> 1; var mid = (low + high) >> 1;
@@ -439,7 +439,7 @@
// Memoize an expensive function by storing its results. // Memoize an expensive function by storing its results.
_.memoize = function(func, hasher) { _.memoize = function(func, hasher) {
var memo = {}; var memo = {};
hasher = hasher || _.identity; hasher || (hasher = _.identity);
return function() { return function() {
var key = hasher.apply(this, arguments); var key = hasher.apply(this, arguments);
return hasOwnProperty.call(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments)); return hasOwnProperty.call(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));