0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 12:10:08 +01:00
nodejs/benchmark/url/whatwg-url-properties.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

60 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common.js');
const bench = common.createBenchmark(main, {
withBase: ['true', 'false'],
type: ['wpt'], // Too many combinations - just use WPT by default
e: [1],
prop: ['href', 'origin', 'protocol',
'username', 'password', 'host', 'hostname', 'port',
'pathname', 'search', 'searchParams', 'hash'],
});
function setAndGet(data, prop) {
const len = data.length;
let result = data[0][prop];
bench.start();
for (let i = 0; i < len; ++i) {
result = data[i][prop];
data[i][prop] = result;
}
bench.end(len);
return result;
}
function get(data, prop) {
const len = data.length;
let result = data[0][prop];
bench.start();
for (let i = 0; i < len; ++i) {
result = data[i][prop]; // get
}
bench.end(len);
return result;
}
function main({ e, type, prop, withBase }) {
withBase = withBase === 'true';
const data = common.bakeUrlData(type, e, withBase, true);
switch (prop) {
case 'protocol':
case 'username':
case 'password':
case 'host':
case 'hostname':
case 'port':
case 'pathname':
case 'search':
case 'hash':
case 'href':
setAndGet(data, prop);
break;
case 'origin':
case 'searchParams':
get(data, prop);
break;
default:
throw new Error('Unknown prop');
}
}