8000 Optimize `_.pluck` for modern environments. · lodash/lodash@80e0e3f · GitHub
[go: up one dir, main page]

Skip to content

Commit 80e0e3f

Browse files
committed
Optimize _.pluck for modern environments.
Former-commit-id: 1a6bbb9866b8ac20faaa707be5f33814579e1df8
1 parent 2cc2d69 commit 80e0e3f

File tree

5 files changed

+103
-63
lines changed

5 files changed

+103
-63
lines changed

build.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,22 @@
18951895
'}'
18961896
].join('\n'));
18971897

1898+
// replace `_.pluck`
1899+
source = replaceFunction(source, 'pluck', [
1900+
'function pluck(collection, property) {',
1901+
' var index = -1,',
1902+
' length = collection ? collection.length : 0;',
1903+
'',
1904+
" if (typeof length == 'number') {",
1905+
' var result = Array(length);',
1906+
' while (++index < length) {',
1907+
' result[index] = collection[index][property]',
1908+
' }',
1909+
' }',
1910+
' return result || map(collection, property);',
1911+
'}'
1912+
].join('\n'));
1913+
18981914
// replace `isArray(collection)` checks in "Collections" methods with simpler type checks
18991915
_.each(['every', 'filter', 'find', 'max', 'min', 'reduce', 'some'], function(methodName) {
19001916
source = source.replace(matchFunction(source, methodName), function(match) {

dist/lodash.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2667,7 +2667,18 @@
26672667
* _.pluck(stooges, 'name');
26682668
* // => ['moe', 'larry']
26692669
*/
2670-
var pluck = map;
2670+
function pluck(collection, property) {
2671+
var index = -1,
2672+
length = collection ? collection.length : 0;
2673+
2674+
if (typeof length == 'number') {
2675+
var result = Array(length);
2676+
while (++index < length) {
2677+
result[index] = collection[index][property]
2678+
}
2679+
}
2680+
return result || map(collection, property);
2681+
}
26712682

26722683
/**
26732684
* Reduces a `collection` to a value that is the accumulated result of running

0 commit comments

Comments
 (0)
0