Skip to content

Commit

Permalink
Optimize _.invoke for arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Feb 3, 2014
1 parent 79b88f4 commit a1b2058
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 193 deletions.
17 changes: 12 additions & 5 deletions dist/lodash.compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3859,15 +3859,22 @@
* // => [['1', '2', '3'], ['4', '5', '6']]
*/
function invoke(collection, methodName) {
var args = slice(arguments, 2),
index = -1,
var index = -1,
isFunc = typeof methodName == 'function',
length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0);

baseEach(collection, function(value) {
result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
});
if (arguments.length < 3 && isArray(collection)) {
while (++index < length) {
var value = collection[index];
result[index] = isFunc ? methodName.call(value) : value[methodName]();
}
} else {
var args = slice(arguments, 2);
baseEach(collection, function(value) {
result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
});
}
return result;
}

Expand Down
26 changes: 13 additions & 13 deletions dist/lodash.compat.min.js

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions dist/lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -3596,15 +3596,22 @@
* // => [['1', '2', '3'], ['4', '5', '6']]
*/
function invoke(collection, methodName) {
var args = slice(arguments, 2),
index = -1,
var index = -1,
isFunc = typeof methodName == 'function',
length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0);

baseEach(collection, function(value) {
result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
});
if (arguments.length < 3 && typeof length == 'number') {
while (++index < length) {
var value = collection[index];
result[index] = isFunc ? methodName.call(value) : value[methodName]();
}
} else {
var args = slice(arguments, 2);
baseEach(collection, function(value) {
result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
});
}
return result;
}

Expand Down
61 changes: 31 additions & 30 deletions dist/lodash.min.js

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions dist/lodash.underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2481,15 +2481,22 @@
* // => [['1', '2', '3'], ['4', '5', '6']]
*/
function invoke(collection, methodName) {
var args = slice(arguments, 2),
index = -1,
var index = -1,
isFunc = typeof methodName == 'function',
length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0);

baseEach(collection, function(value) {
result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
});
if (arguments.length < 3 && typeof length == 'number') {
while (++index < length) {
var value = collection[index];
result[index] = isFunc ? methodName.call(value) : value[methodName]();
}
} else {
var args = slice(arguments, 2);
baseEach(collection, function(value) {
result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
});
}
return result;
}

Expand Down
70 changes: 35 additions & 35 deletions dist/lodash.underscore.min.js

Large diffs are not rendered by default.

178 changes: 89 additions & 89 deletions doc/README.md

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -3875,15 +3875,22 @@
* // => [['1', '2', '3'], ['4', '5', '6']]
*/
function invoke(collection, methodName) {
var args = slice(arguments, 2),
index = -1,
var index = -1,
isFunc = typeof methodName == 'function',
length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0);

baseEach(collection, function(value) {
result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
});
if (arguments.length < 3 && isArray(collection)) {
while (++index < length) {
var value = collection[index];
result[index] = isFunc ? methodName.call(value) : value[methodName]();
}
} else {
var args = slice(arguments, 2);
baseEach(collection, function(value) {
result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
});
}
return result;
}

Expand Down
22 changes: 16 additions & 6 deletions perf/perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -1228,30 +1228,40 @@
suites.push(
Benchmark.Suite('`_.invoke` iterating an array')
.add(buildName, '\
lodash.invoke(numbers, "toFixed", "2")'
lodash.invoke(numbers, "toFixed")'
)
.add(otherName, '\
_.invoke(numbers, "toFixed", "2")'
_.invoke(numbers, "toFixed")'
)
);

suites.push(
Benchmark.Suite('`_.invoke` with arguments iterating an array')
.add(buildName, '\
lodash.invoke(numbers, "toFixed", 1)'
)
.add(otherName, '\
_.invoke(numbers, "toFixed", 1)'
)
);

suites.push(
Benchmark.Suite('`_.invoke` with a function for `methodName` iterating an array')
.add(buildName, '\
lodash.invoke(numbers, String.prototype.split, "")'
lodash.invoke(numbers, Number.prototype.toFixed, 1)'
)
.add(otherName, '\
_.invoke(numbers, String.prototype.split, "")'
_.invoke(numbers, Number.prototype.toFixed, 1)'
)
);

suites.push(
Benchmark.Suite('`_.invoke` iterating an object')
.add(buildName, '\
lodash.invoke(object, "toFixed", "2")'
lodash.invoke(object, "toFixed", 1)'
)
.add(otherName, '\
_.invoke(object, "toFixed", "2")'
_.invoke(object, "toFixed", 1)'
)
);

Expand Down

0 comments on commit a1b2058

Please sign in to comment.