Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor benchmark and add no-var eslint rule #31794

Closed
wants to merge 11 commits into from
1 change: 1 addition & 0 deletions benchmark/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ env:
es6: true

rules:
no-var: error
comma-dangle:
- error
- arrays: 'always-multiline'
Expand Down
2 changes: 1 addition & 1 deletion benchmark/async_hooks/async-resource-vs-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function buildCurrentResource(getServe) {
}

function init(asyncId, type, triggerAsyncId, resource) {
var cr = executionAsyncResource();
const cr = executionAsyncResource();
if (cr !== null) {
resource[cls] = cr[cls];
}
Expand Down
2 changes: 1 addition & 1 deletion benchmark/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function formatResult(data) {
conf += ` ${key}=${JSON.stringify(data.conf[key])}`;
}

var rate = data.rate.toString().split('.');
let rate = data.rate.toString().split('.');
rate[0] = rate[0].replace(/(\d)(?=(?:\d\d\d)+(?!\d))/g, '$1,');
rate = (rate[1] ? rate.join('.') : rate[0]);
return `${data.name}${conf}: ${rate}`;
Expand Down
2 changes: 1 addition & 1 deletion benchmark/dns/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const bench = common.createBenchmark(main, {
});

function main({ name, n, all }) {
var i = 0;
let i = 0;

if (all === 'true') {
const opts = { all: true };
Expand Down
2 changes: 1 addition & 1 deletion benchmark/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ if (format === 'csv') {
conf = conf.replace(/"/g, '""');
console.log(`"${data.name}", "${conf}", ${data.rate}, ${data.time}`);
} else {
var rate = data.rate.toString().split('.');
let rate = data.rate.toString().split('.');
rate[0] = rate[0].replace(/(\d)(?=(?:\d\d\d)+(?!\d))/g, '$1,');
rate = (rate[1] ? rate.join('.') : rate[0]);
console.log(`${data.name} ${conf}: ${rate}`);
Expand Down
10 changes: 5 additions & 5 deletions benchmark/timers/immediate.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function main({ n, type }) {

// setImmediate tail recursion, 0 arguments
function depth(N) {
var n = 0;
let n = 0;
bench.start();
setImmediate(cb);
function cb() {
Expand All @@ -45,7 +45,7 @@ function depth(N) {

// setImmediate tail recursion, 1 argument
function depth1(N) {
var n = 0;
let n = 0;
bench.start();
setImmediate(cb, 1);
function cb(a1) {
Expand All @@ -59,7 +59,7 @@ function depth1(N) {

// Concurrent setImmediate, 0 arguments
function breadth(N) {
var n = 0;
let n = 0;
bench.start();
function cb() {
n++;
Expand All @@ -73,7 +73,7 @@ function breadth(N) {

// Concurrent setImmediate, 1 argument
function breadth1(N) {
var n = 0;
let n = 0;
bench.start();
function cb(a1) {
n++;
Expand All @@ -88,7 +88,7 @@ function breadth1(N) {
// Concurrent setImmediate, 4 arguments
function breadth4(N) {
N /= 2;
var n = 0;
let n = 0;
bench.start();
function cb(a1, a2, a3, a4) {
n++;
Expand Down
2 changes: 1 addition & 1 deletion benchmark/timers/timers-breadth-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const bench = common.createBenchmark(main, {
});

function main({ n }) {
var j = 0;
let j = 0;
function cb1(arg1) {
j++;
if (j === n)
Expand Down
2 changes: 1 addition & 1 deletion benchmark/timers/timers-breadth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const bench = common.createBenchmark(main, {
});

function main({ n }) {
var j = 0;
let j = 0;
bench.start();
function cb() {
j++;
Expand Down
4 changes: 2 additions & 2 deletions benchmark/timers/timers-cancel-pooled.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const bench = common.createBenchmark(main, {

function main({ n }) {

var timer = setTimeout(() => {}, 1);
let timer = setTimeout(() => {}, 1);
for (let i = 0; i < n; i++) {
setTimeout(cb, 1);
}
var next = timer._idlePrev;
let next = timer._idlePrev;
clearTimeout(timer);

bench.start();
Expand Down
5 changes: 2 additions & 3 deletions benchmark/timers/timers-cancel-unpooled.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ function main({ n, direction }) {
timersList.push(setTimeout(cb, i + 1));
}

var j;
bench.start();
if (direction === 'start') {
for (j = 0; j < n; j++) {
for (let j = 0; j < n; j++) {
clearTimeout(timersList[j]);
}
} else {
for (j = n - 1; j >= 0; j--) {
for (let j = n - 1; j >= 0; j--) {
clearTimeout(timersList[j]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion benchmark/timers/timers-depth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const bench = common.createBenchmark(main, {
});

function main({ n }) {
var i = 0;
let i = 0;
bench.start();
setTimeout(cb, 1);
function cb() {
Expand Down
5 changes: 2 additions & 3 deletions benchmark/timers/timers-insert-unpooled.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ const bench = common.createBenchmark(main, {
function main({ direction, n }) {
const timersList = [];

var i;
bench.start();
if (direction === 'start') {
for (i = 1; i <= n; i++) {
for (let i = 1; i <= n; i++) {
timersList.push(setTimeout(cb, i));
}
} else {
for (i = n; i > 0; i--) {
for (let i = n; i > 0; i--) {
timersList.push(setTimeout(cb, i));
}
}
Expand Down
2 changes: 1 addition & 1 deletion benchmark/tls/convertprotocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const bench = common.createBenchmark(main, {

function main({ n }) {
const input = ['ABC', 'XYZ123', 'FOO'];
var m = {};
let m = {};
// First call dominates results
if (n > 1) {
tls.convertALPNProtocols(input, m);
Expand Down
10 changes: 5 additions & 5 deletions benchmark/tls/throughput.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const bench = common.createBenchmark(main, {
});

const fixtures = require('../../test/common/fixtures');
var options;
let options;
const tls = require('tls');

function main({ dur, type, size }) {
var encoding;
var chunk;
let encoding;
let chunk;
switch (type) {
case 'buf':
chunk = Buffer.alloc(size, 'b');
Expand All @@ -37,7 +37,7 @@ function main({ dur, type, size }) {
};

const server = tls.createServer(options, onConnection);
var conn;
let conn;
server.listen(common.PORT, () => {
const opt = { port: common.PORT, rejectUnauthorized: false };
conn = tls.connect(opt, () => {
Expand All @@ -52,7 +52,7 @@ function main({ dur, type, size }) {
}
});

var received = 0;
let received = 0;
function onConnection(conn) {
conn.on('data', (chunk) => {
received += chunk.length;
Expand Down
10 changes: 5 additions & 5 deletions benchmark/tls/tls-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const bench = common.createBenchmark(main, {
dur: [5]
});

var clientConn = 0;
var serverConn = 0;
var dur;
var concurrency;
var running = true;
let clientConn = 0;
let serverConn = 0;
let dur;
let concurrency;
let running = true;

function main(conf) {
dur = conf.dur;
Expand Down
10 changes: 5 additions & 5 deletions benchmark/url/legacy-vs-whatwg-url-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const bench = common.createBenchmark(main, {

function useLegacy(data) {
const len = data.length;
var result = url.parse(data[0]); // Avoid dead code elimination
let result = url.parse(data[0]); // Avoid dead code elimination
bench.start();
for (let i = 0; i < len; ++i) {
result = url.parse(data[i]);
Expand All @@ -24,7 +24,7 @@ function useLegacy(data) {

function useWHATWGWithBase(data) {
const len = data.length;
var result = new URL(data[0][0], data[0][1]); // Avoid dead code elimination
let result = new URL(data[0][0], data[0][1]); // Avoid dead code elimination
bench.start();
for (let i = 0; i < len; ++i) {
const item = data[i];
Expand All @@ -36,7 +36,7 @@ function useWHATWGWithBase(data) {

function useWHATWGWithoutBase(data) {
const len = data.length;
var result = new URL(data[0]); // Avoid dead code elimination
let result = new URL(data[0]); // Avoid dead code elimination
bench.start();
for (let i = 0; i < len; ++i) {
result = new URL(data[i]);
Expand All @@ -47,8 +47,8 @@ function useWHATWGWithoutBase(data) {

function main({ e, method, type, withBase }) {
withBase = withBase === 'true';
var noDead; // Avoid dead code elimination.
var data;
let noDead; // Avoid dead code elimination.
let data;
switch (method) {
case 'legacy':
data = common.bakeUrlData(type, e, false, false);
Expand Down
6 changes: 3 additions & 3 deletions benchmark/url/legacy-vs-whatwg-url-serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const bench = common.createBenchmark(main, {
function useLegacy(data) {
const obj = url.parse(data[0]);
const len = data.length;
var noDead = url.format(obj);
let noDead = url.format(obj);
bench.start();
for (let i = 0; i < len; i++) {
noDead = data[i].toString();
Expand All @@ -25,7 +25,7 @@ function useLegacy(data) {
function useWHATWG(data) {
const obj = new URL(data[0]);
const len = data.length;
var noDead = obj.toString();
let noDead = obj.toString();
bench.start();
for (let i = 0; i < len; i++) {
noDead = data[i].toString();
Expand All @@ -37,7 +37,7 @@ function useWHATWG(data) {
function main({ type, e, method }) {
const data = common.bakeUrlData(type, e, false, false);

var noDead; // Avoid dead code elimination.
let noDead; // Avoid dead code elimination.
switch (method) {
case 'legacy':
noDead = useLegacy(data);
Expand Down
4 changes: 2 additions & 2 deletions benchmark/url/whatwg-url-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const bench = common.createBenchmark(main, {

function setAndGet(data, prop) {
const len = data.length;
var result = data[0][prop];
let result = data[0][prop];
bench.start();
for (let i = 0; i < len; ++i) {
result = data[i][prop];
Expand All @@ -24,7 +24,7 @@ function setAndGet(data, prop) {

function get(data, prop) {
const len = data.length;
var result = data[0][prop];
let result = data[0][prop];
bench.start();
for (let i = 0; i < len; ++i) {
result = data[i][prop]; // get
Expand Down
8 changes: 4 additions & 4 deletions benchmark/util/inspect-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const bench = common.createBenchmark(main, {
});

function main({ n, len, type }) {
var arr = Array(len);
var i, opts;
let arr = Array(len);
let opts;

switch (type) {
case 'denseArray_showHidden':
Expand All @@ -31,14 +31,14 @@ function main({ n, len, type }) {
case 'sparseArray':
break;
case 'mixedArray':
for (i = 0; i < n; i += 2)
for (let i = 0; i < n; i += 2)
arr[i] = i;
break;
default:
throw new Error(`Unsupported type ${type}`);
}
bench.start();
for (i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
util.inspect(arr, opts);
}
bench.end(n);
Expand Down
2 changes: 1 addition & 1 deletion benchmark/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function benchmark(n, obj, options) {
}

function main({ method, n, option }) {
var obj;
let obj;
const options = opts[option];
switch (method) {
case 'Object':
Expand Down
6 changes: 3 additions & 3 deletions benchmark/util/normalize-encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ function getInput(input) {
function main({ input, n }) {
const { normalizeEncoding } = require('internal/util');
const inputs = getInput(input);
var noDead = '';
let noDead = '';

bench.start();
for (var i = 0; i < n; ++i) {
for (var j = 0; j < inputs.length; ++j) {
for (let i = 0; i < n; ++i) {
for (let j = 0; j < inputs.length; ++j) {
noDead = normalizeEncoding(inputs[j]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion benchmark/worker/echo.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function main({ n, workers, sendsPerBroadcast: sends, payload: payloadType }) {
return;
}
for (const worker of workerObjs) {
for (var i = 0; i < sends; ++i)
for (let i = 0; i < sends; ++i)
worker.postMessage(payload);
}
}
Expand Down
5 changes: 2 additions & 3 deletions benchmark/zlib/creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ function main({ n, type, options }) {
const fn = zlib[`create${type}`];
if (typeof fn !== 'function')
throw new Error('Invalid zlib type');
var i = 0;

if (options === 'true') {
const opts = {};
bench.start();
for (; i < n; ++i)
for (let i = 0; i < n; ++i)
fn(opts);
bench.end(n);
} else {
bench.start();
for (; i < n; ++i)
for (let i = 0; i < n; ++i)
fn();
bench.end(n);
}
Expand Down
Loading