Skip to content

Commit 142a92f

Browse files
committed
benchmark: refactor path benchmarks
So far the benchmarks created a highly specialized function which would inline exactly to the input. This changes it to provide a more realistic view to actual input by changing the input on each iteration. That prevents the function to be to specific. It also reduces the number of iterations the benchmarks are run to reduce the overall runtime. A microbenchmark should already show a significant difference with lower iterations, otherwise the significance for real world applications is only limited. PR-URL: #26359 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b45c22b commit 142a92f

21 files changed

+86
-62
lines changed

Diff for: benchmark/path/basename-posix.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const bench = common.createBenchmark(main, {
1515
'/foo/bar/baz/asdf/quux.html',
1616
['/foo/bar/baz/asdf/quux.html', '.html'].join('|'),
1717
],
18-
n: [1e6]
18+
n: [1e5]
1919
});
2020

2121
function main({ n, pathext }) {
@@ -28,7 +28,7 @@ function main({ n, pathext }) {
2828

2929
bench.start();
3030
for (var i = 0; i < n; i++) {
31-
posix.basename(pathext, ext);
31+
posix.basename(i % 3 === 0 ? `${pathext}${i}` : pathext, ext);
3232
}
3333
bench.end(n);
3434
}

Diff for: benchmark/path/basename-win32.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const bench = common.createBenchmark(main, {
1515
'\\foo\\bar\\baz\\asdf\\quux.html',
1616
['\\foo\\bar\\baz\\asdf\\quux.html', '.html'].join('|'),
1717
],
18-
n: [1e6]
18+
n: [1e5]
1919
});
2020

2121
function main({ n, pathext }) {
@@ -28,7 +28,7 @@ function main({ n, pathext }) {
2828

2929
bench.start();
3030
for (var i = 0; i < n; i++) {
31-
win32.basename(pathext, ext);
31+
win32.basename(i % 3 === 0 ? `${pathext}${i}` : pathext, ext);
3232
}
3333
bench.end(n);
3434
}

Diff for: benchmark/path/dirname-posix.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ const bench = common.createBenchmark(main, {
1212
'foo/bar',
1313
'/foo/bar/baz/asdf/quux',
1414
],
15-
n: [1e6]
15+
n: [1e5]
1616
});
1717

1818
function main({ n, path }) {
1919
bench.start();
2020
for (var i = 0; i < n; i++) {
21-
posix.dirname(path);
21+
posix.dirname(i % 3 === 0 ? `${path}${i}` : path);
2222
}
2323
bench.end(n);
2424
}

Diff for: benchmark/path/dirname-win32.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ const bench = common.createBenchmark(main, {
1212
'foo\\bar',
1313
'D:\\foo\\bar\\baz\\asdf\\quux',
1414
],
15-
n: [1e6]
15+
n: [1e5]
1616
});
1717

1818
function main({ n, path }) {
1919
bench.start();
2020
for (var i = 0; i < n; i++) {
21-
win32.dirname(path);
21+
win32.dirname(i % 3 === 0 ? `${path}${i}` : path);
2222
}
2323
bench.end(n);
2424
}

Diff for: benchmark/path/extname-posix.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ const bench = common.createBenchmark(main, {
1515
'/foo/bar/baz/asdf/quux',
1616
'/foo/bar/baz/asdf/quux.foobarbazasdfquux',
1717
],
18-
n: [1e6]
18+
n: [1e5]
1919
});
2020

2121
function main({ n, path }) {
2222
bench.start();
2323
for (var i = 0; i < n; i++) {
24-
posix.extname(path);
24+
posix.extname(i % 3 === 0 ? `${path}${i}` : path);
2525
}
2626
bench.end(n);
2727
}

Diff for: benchmark/path/extname-win32.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ const bench = common.createBenchmark(main, {
1515
'D:\\foo\\bar\\baz\\asdf\\quux',
1616
'\\foo\\bar\\baz\\asdf\\quux.foobarbazasdfquux',
1717
],
18-
n: [1e6]
18+
n: [1e5]
1919
});
2020

2121
function main({ n, path }) {
2222
bench.start();
2323
for (var i = 0; i < n; i++) {
24-
win32.extname(path);
24+
win32.extname(i % 3 === 0 ? `${path}${i}` : path);
2525
}
2626
bench.end(n);
2727
}

Diff for: benchmark/path/format-posix.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ const bench = common.createBenchmark(main, {
66
props: [
77
['/', '/home/user/dir', 'index.html', '.html', 'index'].join('|'),
88
],
9-
n: [1e7]
9+
n: [1e6]
1010
});
1111

1212
function main({ n, props }) {
1313
props = props.split('|');
1414
const obj = {
1515
root: props[0] || '',
1616
dir: props[1] || '',
17-
base: props[2] || '',
17+
base: '',
1818
ext: props[3] || '',
19-
name: props[4] || '',
19+
name: '',
2020
};
2121

2222
bench.start();
2323
for (var i = 0; i < n; i++) {
24+
obj.base = `a${i}${props[2] || ''}`;
25+
obj.name = `a${i}${props[4] || ''}`;
2426
posix.format(obj);
2527
}
2628
bench.end(n);

Diff for: benchmark/path/format-win32.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ const bench = common.createBenchmark(main, {
66
props: [
77
['C:\\', 'C:\\path\\dir', 'index.html', '.html', 'index'].join('|'),
88
],
9-
n: [1e7]
9+
n: [1e6]
1010
});
1111

1212
function main({ n, props }) {
1313
props = props.split('|');
1414
const obj = {
1515
root: props[0] || '',
1616
dir: props[1] || '',
17-
base: props[2] || '',
17+
base: '',
1818
ext: props[3] || '',
19-
name: props[4] || '',
19+
name: '',
2020
};
2121

2222
bench.start();
2323
for (var i = 0; i < n; i++) {
24+
obj.base = `a${i}${props[2] || ''}`;
25+
obj.name = `a${i}${props[4] || ''}`;
2426
win32.format(obj);
2527
}
2628
bench.end(n);

Diff for: benchmark/path/isAbsolute-posix.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ const bench = common.createBenchmark(main, {
1010
'/baz/..',
1111
'bar/baz',
1212
],
13-
n: [1e6]
13+
n: [1e5]
1414
});
1515

1616
function main({ n, path }) {
1717
bench.start();
1818
for (var i = 0; i < n; i++) {
19-
posix.isAbsolute(path);
19+
posix.isAbsolute(i % 3 === 0 ? `${path}${i}` : path);
2020
}
2121
bench.end(n);
2222
}

Diff for: benchmark/path/isAbsolute-win32.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ const bench = common.createBenchmark(main, {
1111
'C:baz\\..',
1212
'bar\\baz',
1313
],
14-
n: [1e6]
14+
n: [1e5]
1515
});
1616

1717
function main({ n, path }) {
1818
bench.start();
1919
for (var i = 0; i < n; i++) {
20-
win32.isAbsolute(path);
20+
win32.isAbsolute(i % 3 === 0 ? `${path}${i}` : path);
2121
}
2222
bench.end(n);
2323
}

Diff for: benchmark/path/join-posix.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@ const bench = common.createBenchmark(main, {
66
paths: [
77
['/foo', 'bar', '', 'baz/asdf', 'quux', '..'].join('|'),
88
],
9-
n: [1e6]
9+
n: [1e5]
1010
});
1111

1212
function main({ n, paths }) {
1313
const args = paths.split('|');
14+
const copy = [...args];
15+
const orig = copy[1];
1416

1517
bench.start();
1618
for (var i = 0; i < n; i++) {
17-
posix.join.apply(null, args);
19+
if (i % 3 === 0) {
20+
copy[1] = `${orig}${i}`;
21+
posix.join(...copy);
22+
} else {
23+
posix.join(...args);
24+
}
1825
}
1926
bench.end(n);
2027
}

Diff for: benchmark/path/join-win32.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@ const bench = common.createBenchmark(main, {
66
paths: [
77
['C:\\foo', 'bar', '', 'baz\\asdf', 'quux', '..'].join('|'),
88
],
9-
n: [1e6]
9+
n: [1e5]
1010
});
1111

1212
function main({ n, paths }) {
1313
const args = paths.split('|');
14+
const copy = [...args];
15+
const orig = copy[1];
1416

1517
bench.start();
1618
for (var i = 0; i < n; i++) {
17-
win32.join.apply(null, args);
19+
if (i % 3 === 0) {
20+
copy[1] = `${orig}${i}`;
21+
win32.join(...copy);
22+
} else {
23+
win32.join(...args);
24+
}
1825
}
1926
bench.end(n);
2027
}

Diff for: benchmark/path/makeLong-win32.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ const bench = common.createBenchmark(main, {
99
'\\\\foo\\bar',
1010
'\\\\?\\foo',
1111
],
12-
n: [1e6]
12+
n: [1e5]
1313
});
1414

1515
function main({ n, path }) {
1616
bench.start();
1717
for (var i = 0; i < n; i++) {
18-
win32._makeLong(path);
18+
win32._makeLong(i % 3 === 0 ? `${path}${i}` : path);
1919
}
2020
bench.end(n);
2121
}

Diff for: benchmark/path/normalize-posix.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ const bench = common.createBenchmark(main, {
1111
'/foo/bar',
1212
'/foo/bar//baz/asdf/quux/..',
1313
],
14-
n: [1e6]
14+
n: [1e5]
1515
});
1616

1717
function main({ n, path }) {
1818
bench.start();
1919
for (var i = 0; i < n; i++) {
20-
posix.normalize(path);
20+
posix.normalize(i % 3 === 0 ? `${path}${i}` : path);
2121
}
2222
bench.end(n);
2323
}

Diff for: benchmark/path/normalize-win32.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ const bench = common.createBenchmark(main, {
1111
'C:\\foo\\bar',
1212
'C:\\foo\\bar\\\\baz\\asdf\\quux\\..',
1313
],
14-
n: [1e6]
14+
n: [1e5]
1515
});
1616

1717
function main({ n, path }) {
1818
bench.start();
1919
for (var i = 0; i < n; i++) {
20-
win32.normalize(path);
20+
win32.normalize(i % 3 === 0 ? `${path}${i}` : path);
2121
}
2222
bench.end(n);
2323
}

Diff for: benchmark/path/parse-posix.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@ const bench = common.createBenchmark(main, {
1212
'foo/bar',
1313
'/foo/bar/baz/asdf/.quux',
1414
],
15-
n: [1e6]
15+
n: [1e5]
1616
});
1717

1818
function main({ n, path }) {
19-
for (var i = 0; i < n; i++) {
20-
posix.parse(path);
21-
}
2219
bench.start();
23-
for (i = 0; i < n; i++) {
24-
posix.parse(path);
20+
for (let i = 0; i < n; i++) {
21+
posix.parse(i % 3 === 0 ? `${path}${i}` : path);
2522
}
2623
bench.end(n);
2724
}

Diff for: benchmark/path/parse-win32.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@ const bench = common.createBenchmark(main, {
1313
'foo\\bar',
1414
'\\foo\\bar\\baz\\asdf\\.quux',
1515
],
16-
n: [1e6]
16+
n: [1e5]
1717
});
1818

1919
function main({ n, path }) {
20-
for (var i = 0; i < n; i++) {
21-
win32.parse(path);
22-
}
2320
bench.start();
24-
for (i = 0; i < n; i++) {
25-
win32.parse(path);
21+
for (let i = 0; i < n; i++) {
22+
win32.parse(i % 3 === 0 ? `${path}${i}` : path);
2623
}
2724
bench.end(n);
2825
}

Diff for: benchmark/path/relative-posix.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const bench = common.createBenchmark(main, {
1212
['/foo/bar/baz/quux', '/foo/bar/baz/quux'].join('|'),
1313
['/foo/bar/baz/quux', '/var/log'].join('|'),
1414
],
15-
n: [1e6]
15+
n: [1e5]
1616
});
1717

1818
function main({ n, paths }) {
@@ -22,13 +22,13 @@ function main({ n, paths }) {
2222
to = paths.slice(delimIdx + 1);
2323
paths = paths.slice(0, delimIdx);
2424
}
25-
for (var i = 0; i < n; i++) {
26-
posix.relative(paths, to);
27-
}
2825

2926
bench.start();
30-
for (i = 0; i < n; i++) {
31-
posix.relative(paths, to);
27+
for (let i = 0; i < n; i++) {
28+
if (i % 3 === 0)
29+
posix.relative(`${paths}${i}`, `${to}${i}`);
30+
else
31+
posix.relative(paths, to);
3232
}
3333
bench.end(n);
3434
}

Diff for: benchmark/path/relative-win32.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const bench = common.createBenchmark(main, {
1010
['C:\\foo\\BAR\\BAZ', 'C:\\foo\\bar\\baz'].join('|'),
1111
['C:\\foo\\bar\\baz\\quux', 'C:\\'].join('|'),
1212
],
13-
n: [1e6]
13+
n: [1e5]
1414
});
1515

1616
function main({ n, paths }) {
@@ -21,14 +21,12 @@ function main({ n, paths }) {
2121
paths = paths.slice(0, delimIdx);
2222
}
2323

24-
// Warmup
25-
for (var i = 0; i < n; i++) {
26-
win32.relative(paths, to);
27-
}
28-
2924
bench.start();
30-
for (i = 0; i < n; i++) {
31-
win32.relative(paths, to);
25+
for (let i = 0; i < n; i++) {
26+
if (i % 3 === 0)
27+
win32.relative(`${paths}${i}`, `${to}${i}`);
28+
else
29+
win32.relative(paths, to);
3230
}
3331
bench.end(n);
3432
}

0 commit comments

Comments
 (0)