benchmark: fix benchmark for file path and URL conversion

PR-URL: https://github.com/nodejs/node/pull/54190
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Early Riser 2024-08-25 09:41:13 -04:00 committed by GitHub
parent 52322aa42a
commit 43f699d4d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,28 +3,46 @@ const common = require('../common.js');
const { fileURLToPath, pathToFileURL } = require('node:url'); const { fileURLToPath, pathToFileURL } = require('node:url');
const isWindows = process.platform === 'win32'; const isWindows = process.platform === 'win32';
const bench = common.createBenchmark(main, { const inputs = isWindows ? [
input: isWindows ? [ 'C:\\foo',
'file:///c/', 'C:\\Program Files\\Music\\Web Sys\\main.html?REQUEST=RADIO',
] : [ '\\\\nas\\My Docs\\File.doc',
'file:///dev/null', '\\\\?\\UNC\\server\\share\\folder\\file.txt',
'file:///dev/null?key=param&bool', 'file:///C:/foo',
'file:///dev/null?key=param&bool#hash', 'file:///C:/dir/foo?query=1',
], 'file:///C:/dir/foo#fragment',
method: isWindows ? [ ] : [
'fileURLToPath', '/dev/null',
] : [ '/dev/null?key=param&bool',
'fileURLToPath', '/dev/null?key=param&bool#hash',
'pathToFileURL', 'file:///dev/null',
], 'file:///dev/null?key=param&bool',
n: [5e6], 'file:///dev/null?key=param&bool#hash',
}); ];
function main({ n, input, method }) { const bench = common.createBenchmark(
method = method === 'fileURLOrPath' ? fileURLToPath : pathToFileURL; main,
{
method: ['pathToFileURL', 'fileURLToPath'],
input: Object.values(inputs),
n: [5e6],
},
{
combinationFilter: (p) => (
(isWindows ?
(!p.input.startsWith('file://') && p.method === 'pathToFileURL') :
p.method === 'pathToFileURL'
) ||
(p.input.startsWith('file://') && p.method === 'fileURLToPath')
),
},
);
function main({ method, input, n }) {
const methodFunc = method === 'fileURLToPath' ? fileURLToPath : pathToFileURL;
bench.start(); bench.start();
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
method(input); methodFunc(input);
} }
bench.end(n); bench.end(n);
} }