mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
29357cb0ef
PR-URL: https://github.com/nodejs/node/pull/54988 Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: Raz Luvaton <rluvaton@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
37 lines
850 B
JavaScript
37 lines
850 B
JavaScript
'use strict';
|
|
|
|
const common = require('../common.js');
|
|
const fs = require('fs');
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
encoding: ['undefined', 'utf8', 'ascii'],
|
|
path: ['existing', 'non-existing'],
|
|
hasFileDescriptor: ['true', 'false'],
|
|
n: [1e4],
|
|
});
|
|
|
|
function main({ n, encoding, path, hasFileDescriptor }) {
|
|
const enc = encoding === 'undefined' ? undefined : encoding;
|
|
let file;
|
|
let shouldClose = false;
|
|
|
|
if (hasFileDescriptor === 'true') {
|
|
shouldClose = path === 'existing';
|
|
file = path === 'existing' ? fs.openSync(__filename) : -1;
|
|
} else {
|
|
file = path === 'existing' ? __filename : '/tmp/not-found';
|
|
}
|
|
bench.start();
|
|
for (let i = 0; i < n; ++i) {
|
|
try {
|
|
fs.readFileSync(file, enc);
|
|
} catch {
|
|
// do nothing
|
|
}
|
|
}
|
|
bench.end(n);
|
|
if (shouldClose) {
|
|
fs.closeSync(file);
|
|
}
|
|
}
|