Optimize _.filter.

Former-commit-id: 565c1b0159207e9d9413d82b5827ed685564efff
This commit is contained in:
John-David Dalton
2012-11-10 21:15:25 -08:00
parent 3a68f4cf54
commit 934e585cb3
4 changed files with 65 additions and 52 deletions

View File

@@ -1947,11 +1947,24 @@
function filter(collection, callback, thisArg) {
var result = [];
callback = createCallback(callback, thisArg);
forEach(collection, function(value, index, collection) {
if (callback(value, index, collection)) {
result.push(value);
if (isArray(collection)) {
var index = -1,
length = collection.length;
while (++index < length) {
var value = collection[index];
if (callback(value, index, collection)) {
result.push(value);
}
}
});
} else {
forEach(collection, function(value, index, collection) {
if (callback(value, index, collection)) {
result.push(value);
}
});
}
return result;
}
@@ -2419,7 +2432,7 @@
length = collection.length;
while (++index < length) {
if (result = callback(collection[index], index, collection)) {
if ((result = callback(collection[index], index, collection))) {
break;
}
}