Permalink
Browse files

Optimize `_.pluck` for modern environments.

  • Loading branch information...
jdalton committed Mar 23, 2013
1 parent b4d3b2c commit 247523395536b31fbf8317ce17850181de93db26
Showing with 103 additions and 63 deletions.
  1. +16 −0 build.js
  2. +12 −1 dist/lodash.js
  3. +35 −34 dist/lodash.min.js
  4. +12 −1 dist/lodash.underscore.js
  5. +28 −27 dist/lodash.underscore.min.js
View
@@ -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) {
View
@@ -2667,7 +2667,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
Oops, something went wrong.

0 comments on commit 2475233

Please sign in to comment.