mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 18:37:50 +00:00
Optimize _.filter.
Former-commit-id: 565c1b0159207e9d9413d82b5827ed685564efff
This commit is contained in:
23
lodash.js
23
lodash.js
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user