2016-04-15 18:25:58 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common.js');
|
|
|
|
const assert = require('assert');
|
|
|
|
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
|
|
method: ['withoutdefaults', 'withdefaults'],
|
2023-01-29 19:13:35 +01:00
|
|
|
n: [1e8],
|
2016-04-15 18:25:58 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
function oldStyleDefaults(x, y) {
|
2024-10-09 08:42:16 +02:00
|
|
|
x ||= 1;
|
|
|
|
y ||= 2;
|
2016-04-15 18:25:58 +02:00
|
|
|
assert.strictEqual(x, 1);
|
|
|
|
assert.strictEqual(y, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
function defaultParams(x = 1, y = 2) {
|
|
|
|
assert.strictEqual(x, 1);
|
|
|
|
assert.strictEqual(y, 2);
|
|
|
|
}
|
|
|
|
|
2018-03-17 16:19:09 +01:00
|
|
|
function runOldStyleDefaults(n) {
|
2016-04-15 18:25:58 +02:00
|
|
|
bench.start();
|
2020-01-24 18:12:49 +01:00
|
|
|
for (let i = 0; i < n; i++)
|
2016-04-15 18:25:58 +02:00
|
|
|
oldStyleDefaults();
|
2018-03-17 16:19:09 +01:00
|
|
|
bench.end(n);
|
2016-04-15 18:25:58 +02:00
|
|
|
}
|
|
|
|
|
2018-03-17 16:19:09 +01:00
|
|
|
function runDefaultParams(n) {
|
2016-04-15 18:25:58 +02:00
|
|
|
bench.start();
|
2020-01-24 18:12:49 +01:00
|
|
|
for (let i = 0; i < n; i++)
|
2016-04-15 18:25:58 +02:00
|
|
|
defaultParams();
|
2018-03-17 16:19:09 +01:00
|
|
|
bench.end(n);
|
2016-04-15 18:25:58 +02:00
|
|
|
}
|
|
|
|
|
2018-03-17 16:19:09 +01:00
|
|
|
function main({ n, method }) {
|
2017-12-30 03:59:27 +01:00
|
|
|
switch (method) {
|
2016-04-15 18:25:58 +02:00
|
|
|
case 'withoutdefaults':
|
2018-03-17 16:19:09 +01:00
|
|
|
runOldStyleDefaults(n);
|
2016-04-15 18:25:58 +02:00
|
|
|
break;
|
|
|
|
case 'withdefaults':
|
2018-03-17 16:19:09 +01:00
|
|
|
runDefaultParams(n);
|
2016-04-15 18:25:58 +02:00
|
|
|
break;
|
|
|
|
default:
|
2018-01-23 13:18:30 +01:00
|
|
|
throw new Error(`Unexpected method "${method}"`);
|
2016-04-15 18:25:58 +02:00
|
|
|
}
|
|
|
|
}
|