Skip to content

Commit

Permalink
Avoid Array.prototype.slice.{call,apply}(arguments, ...) in hot code.
Browse files Browse the repository at this point in the history
34 consecutive chewbacca runs says this is a 3-16% improvement.
Median: 14.4%
Average: 11.2%
  • Loading branch information
papandreou committed Nov 8, 2015
1 parent 6dc2e6d commit a049f9a
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/Unexpected.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,13 @@ Unexpected.prototype.fail = function (arg) {
error[key] = additionalProperties[key];
});
} else {
var placeholderArgs = Array.prototype.slice.call(arguments, 1);
var placeholderArgs;
if (arguments.length > 0) {
placeholderArgs = new Array(arguments.length - 1);
for (var i = 1 ; i < arguments.length ; i += 1) {
placeholderArgs[i - 1] = arguments[i];
}
}
error.errorMode = 'bubble';
error.output = function (output) {
var message = arg ? String(arg) : 'Explicit failure';
Expand All @@ -496,7 +502,7 @@ Unexpected.prototype.fail = function (arg) {
var match = placeholderRegexp.exec(token);
if (match) {
var index = match[1];
if (index in placeholderArgs) {
if (placeholderArgs && index in placeholderArgs) {
var placeholderArg = placeholderArgs[index];
if (placeholderArg && placeholderArg.isMagicPen) {
output.append(placeholderArg);
Expand Down Expand Up @@ -1020,7 +1026,10 @@ Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
return Boolean(flags[flag]) !== Boolean(negate) ? flag + ' ' : '';
}).trim();

var args = Array.prototype.slice.call(arguments, 2);
var args = new Array(arguments.length - 2);
for (var i = 2 ; i < arguments.length ; i += 1) {
args[i - 2] = arguments[i];
}
return wrappedExpect.callInNestedContext(function () {
return executeExpect(subject, testDescriptionString, args);
});
Expand Down Expand Up @@ -1061,7 +1070,10 @@ Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
return oathbreaker(assertionRule.handler.apply(wrappedExpect, [wrappedExpect, subject].concat(args)));
}

var args = Array.prototype.slice.call(arguments, 2);
var args = new Array(arguments.length - 2);
for (var i = 2 ; i < arguments.length ; i += 1) {
args[i - 2] = arguments[i];
}
try {
var result = executeExpect(subject, testDescriptionString, args);
if (isPendingPromise(result)) {
Expand Down

0 comments on commit a049f9a

Please sign in to comment.