node/benchmark/fs/readFileSync.js
Rafael Gonzaga 29357cb0ef
benchmark: include ascii to fs/readfile
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>
2024-09-19 20:15:47 +00:00

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);
}
}