Skip to content

Commit b9832eb

Browse files
trevnorrisbzoz
authored andcommitted
bench: add bench for fs.realpath() fix
The benchmarks included also work for the previous JS implementation of fs.realpath(). In case the new implementation of realpath() needs to be reverted, we want these changes to stick around. PR-URL: #7899 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 08996fd commit b9832eb

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

benchmark/fs/bench-realpath.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fs = require('fs');
5+
const path = require('path');
6+
const resolved_path = path.resolve(__dirname, '../../lib/');
7+
const relative_path = path.relative(__dirname, '../../lib/');
8+
9+
const bench = common.createBenchmark(main, {
10+
n: [1e4],
11+
type: ['relative', 'resolved'],
12+
});
13+
14+
15+
function main(conf) {
16+
const n = conf.n >>> 0;
17+
const type = conf.type;
18+
19+
bench.start();
20+
if (type === 'relative')
21+
relativePath(n);
22+
else if (type === 'resolved')
23+
resolvedPath(n);
24+
else
25+
throw new Error('unknown "type": ' + type);
26+
}
27+
28+
function relativePath(n) {
29+
(function r(cntr) {
30+
if (--cntr <= 0)
31+
return bench.end(n);
32+
fs.realpath(relative_path, function() {
33+
r(cntr);
34+
});
35+
}(n));
36+
}
37+
38+
function resolvedPath(n) {
39+
(function r(cntr) {
40+
if (--cntr <= 0)
41+
return bench.end(n);
42+
fs.realpath(resolved_path, function() {
43+
r(cntr);
44+
});
45+
}(n));
46+
}

benchmark/fs/bench-realpathSync.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fs = require('fs');
5+
const path = require('path');
6+
const resolved_path = path.resolve(__dirname, '../../lib/');
7+
const relative_path = path.relative(__dirname, '../../lib/');
8+
9+
const bench = common.createBenchmark(main, {
10+
n: [1e4],
11+
type: ['relative', 'resolved'],
12+
});
13+
14+
15+
function main(conf) {
16+
const n = conf.n >>> 0;
17+
const type = conf.type;
18+
19+
bench.start();
20+
if (type === 'relative')
21+
relativePath(n);
22+
else if (type === 'resolved')
23+
resolvedPath(n);
24+
else
25+
throw new Error('unknown "type": ' + type);
26+
bench.end(n);
27+
}
28+
29+
function relativePath(n) {
30+
for (var i = 0; i < n; i++) {
31+
fs.realpathSync(relative_path);
32+
}
33+
}
34+
35+
function resolvedPath(n) {
36+
for (var i = 0; i < n; i++) {
37+
fs.realpathSync(resolved_path);
38+
}
39+
}

0 commit comments

Comments
 (0)