Update vendors and add _.reduce, _.reduceRight, and _.where benchmarks.

Former-commit-id: c1b4bc7f8aaf08c429ae918f5d528401f1a66255
This commit is contained in:
John-David Dalton
2012-09-19 21:40:39 -07:00
parent 25de44a23d
commit b91b04f652
5 changed files with 160 additions and 35 deletions

View File

@@ -107,14 +107,18 @@
// **Reduce** builds up a single result from a list of values, aka `inject`,
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context, right) {
var initial = arguments.length > 2;
if (obj == null) obj = [];
if (nativeReduce && obj.reduce === nativeReduce) {
if (!right && nativeReduce && obj.reduce === nativeReduce) {
if (context) iterator = _.bind(iterator, context);
return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
}
each(obj, function(value, index, list) {
if (right) {
index = right.keys[index];
list = right.list;
}
if (!initial) {
memo = value;
initial = true;
@@ -129,15 +133,14 @@
// The right-associative version of reduce, also known as `foldr`.
// Delegates to **ECMAScript 5**'s native `reduceRight` if available.
_.reduceRight = _.foldr = function(obj, iterator, memo, context) {
var initial = arguments.length > 2;
if (obj == null) obj = [];
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
if (context) iterator = _.bind(iterator, context);
return initial ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
return arguments.length > 2 ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
}
var reversed = _.toArray(obj).reverse();
if (context && !initial) iterator = _.bind(iterator, context);
return initial ? _.reduce(reversed, iterator, memo, context) : _.reduce(reversed, iterator);
var keys = _.keys(obj).reverse();
var values = _.toArray(obj).reverse();
return _.reduce(values, iterator, memo, context, {keys: keys, list: obj});
};
// Return the first value which passes a truth test. Aliased as `detect`.
@@ -848,8 +851,13 @@
}
}
} else {
// Objects with different constructors are not equivalent.
if ('constructor' in a != 'constructor' in b || a.constructor != b.constructor) return false;
// Objects with different constructors are not equivalent, but `Object`s
// from different frames are.
var aCtor = a.constructor, bCtor = b.constructor;
if (aCtor !== bCtor && !(_.isFunction(aCtor) && (aCtor instanceof aCtor) &&
_.isFunction(bCtor) && (bCtor instanceof bCtor))) {
return false;
}
// Deep compare objects.
for (var key in a) {
if (_.has(a, key)) {