node/benchmark/fs/bench-unlinkSync.js
CanadaHonk 7e0b6a5939
fs: improve error performance for unlinkSync
PR-URL: https://github.com/nodejs/node/pull/49856
Refs: https://github.com/nodejs/performance/issues/106
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2023-09-27 12:43:05 +00:00

44 lines
908 B
JavaScript

'use strict';
const common = require('../common');
const fs = require('fs');
const tmpdir = require('../../test/common/tmpdir');
tmpdir.refresh();
const bench = common.createBenchmark(main, {
type: ['existing', 'non-existing'],
n: [1e3],
});
function main({ n, type }) {
let files;
switch (type) {
case 'existing':
files = [];
// Populate tmpdir with mock files
for (let i = 0; i < n; i++) {
const path = tmpdir.resolve(`unlinksync-bench-file-${i}`);
fs.writeFileSync(path, 'bench');
files.push(path);
}
break;
case 'non-existing':
files = new Array(n).fill(tmpdir.resolve(`.non-existing-file-${Date.now()}`));
break;
default:
new Error('Invalid type');
}
bench.start();
for (let i = 0; i < n; i++) {
try {
fs.unlinkSync(files[i]);
} catch {
// do nothing
}
}
bench.end(n);
}