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

@@ -1895,6 +1895,22 @@
'}'
].join('\n'));
// replace `_.pluck`
source = replaceFunction(source, 'pluck', [
'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);',
'}'
].join('\n'));
// replace `isArray(collection)` checks in "Collections" methods with simpler type checks
_.each(['every', 'filter', 'find', 'max', 'min', 'reduce', 'some'], function(methodName) {
source = source.replace(matchFunction(source, methodName), function(match) {