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 1cc1f1d
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 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,48 @@ 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/',
] : [
const inputs = isWindows ? {
path: [
'C:\\foo',
'C:\\Program Files\\Music\\Web Sys\\main.html?REQUEST=RADIO',
'\\\\nas\\My Docs\\File.doc',
],
fileUrl: [
'file:///C:/foo',
'file:///C:/dir/foo?query=1',
'file:///C:/dir/foo#fragment',
],
} : {
path: [
'/dev/null',
'/dev/null?key=param&bool',
'/dev/null?key=param&bool#hash',
],
fileUrl: [
'file:///dev/null',
'file:///dev/null?key=param&bool',
'file:///dev/null?key=param&bool#hash',
],
method: isWindows ? [
'fileURLToPath',
] : [
'fileURLToPath',
'pathToFileURL',
],
};

const bench = common.createBenchmark(main, {
type: Object.keys(inputs),
method: ['pathToFileURL', 'fileURLToPath'],
index: [0, 1, 2],
n: [5e6],
}, {
combinationFilter: (p) => {
return p.type === 'path' && p.method === 'pathToFileURL' ||
p.type === 'fileUrl' && p.method === 'fileURLToPath';
},
});

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

0 comments on commit 1cc1f1d

Please sign in to comment.