Skip to content

Commit 27227cf

Browse files
BridgeARMylesBorins
authored andcommitted
benchmark: refactor console benchmark
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]>
1 parent 41e2bb1 commit 27227cf

File tree

3 files changed

+62
-129
lines changed

3 files changed

+62
-129
lines changed

benchmark/misc/arguments.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
3+
const { createBenchmark } = require('../common.js');
4+
const { format } = require('util');
5+
6+
const methods = [
7+
'restAndSpread',
8+
'argumentsAndApply',
9+
'restAndApply',
10+
'predefined'
11+
];
12+
13+
const bench = createBenchmark(main, {
14+
method: methods,
15+
n: [1e6]
16+
});
17+
18+
function usingRestAndSpread(...args) {
19+
format(...args);
20+
}
21+
22+
function usingRestAndApply(...args) {
23+
format.apply(null, args);
24+
}
25+
26+
function usingArgumentsAndApply() {
27+
format.apply(null, arguments);
28+
}
29+
30+
function usingPredefined() {
31+
format('part 1', 'part', 2, 'part 3', 'part', 4);
32+
}
33+
34+
function main({ n, method, args }) {
35+
var fn;
36+
switch (method) {
37+
// '' is a default case for tests
38+
case '':
39+
case 'restAndSpread':
40+
fn = usingRestAndSpread;
41+
break;
42+
case 'restAndApply':
43+
fn = usingRestAndApply;
44+
break;
45+
case 'argumentsAndApply':
46+
fn = usingArgumentsAndApply;
47+
break;
48+
case 'predefined':
49+
fn = usingPredefined;
50+
break;
51+
default:
52+
throw new Error(`Unexpected method "${method}"`);
53+
}
54+
55+
bench.start();
56+
for (var i = 0; i < n; i++)
57+
fn('part 1', 'part', 2, 'part 3', 'part', 4);
58+
bench.end(n);
59+
}

benchmark/misc/console.js

-126
This file was deleted.

lib/console.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ function write(ignoreErrors, stream, string, errorhandler, groupIndent) {
132132
}
133133

134134

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

0 commit comments

Comments
 (0)