0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 07:27:22 +01:00
nodejs/benchmark/url/legacy-vs-whatwg-url-serialize.js
Alex Ramirez 0d1b14ac19 benchmark: swap var for let in url benchmarks
In benchmark url directory this changes for loops using var to let
when it applies for consistency

PR-URL: https://github.com/nodejs/node/pull/28867
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2019-07-30 09:37:48 -07:00

55 lines
1.1 KiB
JavaScript

'use strict';
const common = require('../common.js');
const url = require('url');
const URL = url.URL;
const assert = require('assert');
const bench = common.createBenchmark(main, {
type: common.urlDataTypes,
method: ['legacy', 'whatwg'],
e: [1]
});
function useLegacy(data) {
const obj = url.parse(data[0]);
const len = data.length;
var noDead = url.format(obj);
bench.start();
for (let i = 0; i < len; i++) {
noDead = data[i].toString();
}
bench.end(len);
return noDead;
}
function useWHATWG(data) {
const obj = new URL(data[0]);
const len = data.length;
var noDead = obj.toString();
bench.start();
for (let i = 0; i < len; i++) {
noDead = data[i].toString();
}
bench.end(len);
return noDead;
}
function main({ type, e, method }) {
e = +e;
const data = common.bakeUrlData(type, e, false, false);
var noDead; // Avoid dead code elimination.
switch (method) {
case 'legacy':
noDead = useLegacy(data);
break;
case 'whatwg':
noDead = useWHATWG(data);
break;
default:
throw new Error(`Unknown method ${method}`);
}
assert.ok(noDead);
}