Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2b3ea73

Browse files
committedAug 25, 2024··
benchmark: fix benchmark for file path and URL conversion
1 parent 492032f commit 2b3ea73

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed
 

‎benchmark/url/whatwg-url-to-and-from-path.js

+41-19
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,50 @@ const common = require('../common.js');
33
const { fileURLToPath, pathToFileURL } = require('node:url');
44
const isWindows = process.platform === 'win32';
55

6-
const bench = common.createBenchmark(main, {
7-
input: isWindows ? [
8-
'file:///c/',
9-
] : [
10-
'file:///dev/null',
11-
'file:///dev/null?key=param&bool',
12-
'file:///dev/null?key=param&bool#hash',
13-
],
14-
method: isWindows ? [
15-
'fileURLToPath',
16-
] : [
17-
'fileURLToPath',
18-
'pathToFileURL',
19-
],
20-
n: [5e6],
21-
});
6+
const inputs = isWindows ? [
7+
'C:\\foo',
8+
'C:\\Program Files\\Music\\Web Sys\\main.html?REQUEST=RADIO',
9+
'\\\\nas\\My Docs\\File.doc',
10+
'file:///C:/foo',
11+
'file:///C:/dir/foo?query=1',
12+
'file:///C:/dir/foo#fragment',
13+
] : [
14+
'/dev/null',
15+
'/dev/null?key=param&bool',
16+
'/dev/null?key=param&bool#hash',
17+
'file:///dev/null',
18+
'file:///dev/null?key=param&bool',
19+
'file:///dev/null?key=param&bool#hash',
20+
];
2221

23-
function main({ n, input, method }) {
24-
method = method === 'fileURLOrPath' ? fileURLToPath : pathToFileURL;
22+
const bench = common.createBenchmark(
23+
main,
24+
{
25+
method: ['pathToFileURL', 'fileURLToPath'],
26+
input: Object.values(inputs),
27+
n: [5e6],
28+
},
29+
{
30+
combinationFilter: (p) => {
31+
if (!isWindows) {
32+
return (
33+
(p.method === 'pathToFileURL') ||
34+
(p.input.startsWith('file://') && p.method === 'fileURLToPath')
35+
);
36+
}
37+
return (
38+
(!p.input.startsWith('file://') && p.method === 'pathToFileURL') ||
39+
(p.input.startsWith('file://') && (!isWindows || p.method === 'fileURLToPath'))
40+
);
41+
},
42+
},
43+
);
44+
45+
function main({ method, input, n }) {
46+
const methodFunc = method === 'fileURLToPath' ? fileURLToPath : pathToFileURL;
2547
bench.start();
2648
for (let i = 0; i < n; i++) {
27-
method(input);
49+
methodFunc(input);
2850
}
2951
bench.end(n);
3052
}

0 commit comments

Comments
 (0)
Please sign in to comment.