0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 23:43:09 +01:00
nodejs/benchmark/url/legacy-vs-whatwg-url-serialize.js
Antoine du Hamel 3aef395ed5
benchmark: add trailing commas in benchmark/url
PR-URL: https://github.com/nodejs/node/pull/46551
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Akhil Marsonya <akhil.marsonya27@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
2023-02-10 00:54:40 +00:00

54 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;
let 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;
let 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 }) {
const data = common.bakeUrlData(type, e, false, false);
let 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);
}