Skip to content

Commit

Permalink
benchmark: refactor console benchmark
Browse files Browse the repository at this point in the history
Fix and refactor the console benchmark. It did not test console
so it got renamed and mainly tests the different ways of passing
arguments through.

PR-URL: #17707
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR authored and MylesBorins committed Jan 9, 2018
1 parent 41e2bb1 commit 27227cf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 129 deletions.
59 changes: 59 additions & 0 deletions benchmark/misc/arguments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

const { createBenchmark } = require('../common.js');
const { format } = require('util');

const methods = [
'restAndSpread',
'argumentsAndApply',
'restAndApply',
'predefined'
];

const bench = createBenchmark(main, {
method: methods,
n: [1e6]
});

function usingRestAndSpread(...args) {
format(...args);
}

function usingRestAndApply(...args) {
format.apply(null, args);
}

function usingArgumentsAndApply() {
format.apply(null, arguments);
}

function usingPredefined() {
format('part 1', 'part', 2, 'part 3', 'part', 4);
}

function main({ n, method, args }) {
var fn;
switch (method) {
// '' is a default case for tests
case '':
case 'restAndSpread':
fn = usingRestAndSpread;
break;
case 'restAndApply':
fn = usingRestAndApply;
break;
case 'argumentsAndApply':
fn = usingArgumentsAndApply;
break;
case 'predefined':
fn = usingPredefined;
break;
default:
throw new Error(`Unexpected method "${method}"`);
}

bench.start();
for (var i = 0; i < n; i++)
fn('part 1', 'part', 2, 'part 3', 'part', 4);
bench.end(n);
}
126 changes: 0 additions & 126 deletions benchmark/misc/console.js

This file was deleted.

6 changes: 3 additions & 3 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ function write(ignoreErrors, stream, string, errorhandler, groupIndent) {
}


// As of v8 5.0.71.32, the combination of rest param, template string
// and .apply(null, args) benchmarks consistently faster than using
// the spread operator when calling util.format.
Console.prototype.log = function log(...args) {
write(this._ignoreErrors,
this._stdout,
// The performance of .apply and the spread operator seems on par in V8
// 6.3 but the spread operator, unlike .apply(), pushes the elements
// onto the stack. That is, it makes stack overflows more likely.
util.format.apply(null, args),
this._stdoutErrorHandler,
this[kGroupIndent]);
Expand Down

0 comments on commit 27227cf

Please sign in to comment.