Skip to content

Commit

Permalink
benchmark: fix benchmark for file path and URL conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
EarlyRiser42 committed Aug 4, 2024
1 parent 492032f commit 4377963
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions benchmark/url/whatwg-url-to-and-from-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,44 @@ const common = require('../common.js');
const { fileURLToPath, pathToFileURL } = require('node:url');
const isWindows = process.platform === 'win32';

const bench = common.createBenchmark(main, {
input: isWindows ? [
'file:///c/',
] : [
'file:///dev/null',
'file:///dev/null?key=param&bool',
'file:///dev/null?key=param&bool#hash',
],
method: isWindows ? [
'fileURLToPath',
] : [
'fileURLToPath',
'pathToFileURL',
],
n: [5e6],
});
const inputs = isWindows ? [
'C:\\foo',
'C:\\Program Files\\Music\\Web Sys\\main.html?REQUEST=RADIO',
'\\\\nas\\My Docs\\File.doc',
'file:///C:/foo',
'file:///C:/dir/foo?query=1',
'file:///C:/dir/foo#fragment',
] : [
'/dev/null',
'/dev/null?key=param&bool',
'/dev/null?key=param&bool#hash',
'file:///dev/null',
'file:///dev/null?key=param&bool',
'file:///dev/null?key=param&bool#hash',
];

function main({ n, input, method }) {
method = method === 'fileURLOrPath' ? fileURLToPath : pathToFileURL;
const bench = common.createBenchmark(
main,
{
method: ['pathToFileURL', 'fileURLToPath'],
input: Object.values(inputs).flat(),
n: [5e6],
},
{
combinationFilter: (p) => {
return (
(!p.input.startsWith('file://') && p.method === 'pathToFileURL') ||
(p.input.startsWith('file://') && p.method === 'fileURLToPath')
);
},
},
);

function main({ method, input, n }) {
const methodFunc = method === 'fileURLToPath' ? fileURLToPath : pathToFileURL;
bench.start();
for (let i = 0; i < n; i++) {
method(input);
methodFunc(input);
}
bench.end(n);
}

0 comments on commit 4377963

Please sign in to comment.