Optimize _.pluck for modern environments.

Former-commit-id: 1a6bbb9866b8ac20faaa707be5f33814579e1df8
This commit is contained in:
John-David Dalton
2013-03-23 12:21:43 -07:00
parent 2cc2d696f1
commit 80e0e3fcd7
5 changed files with 103 additions and 63 deletions

View File

@@ -2057,7 +2057,18 @@
* _.pluck(stooges, 'name');
* // => ['moe', 'larry']
*/
var pluck = map;
function pluck(collection, property) {
var index = -1,
length = collection ? collection.length : 0;
if (typeof length == 'number') {
var result = Array(length);
while (++index < length) {
result[index] = collection[index][property]
}
}
return result || map(collection, property);
}
/**
* Reduces a `collection` to a value that is the accumulated result of running