0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-28 14:33:11 +01:00
nodejs/benchmark/fs/bench-writeFileSync.js
CanadaHonk 4466deeb34
fs: add c++ fast path for writeFileSync utf8
PR-URL: https://github.com/nodejs/node/pull/49884
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2023-11-27 00:01:39 +00:00

44 lines
1.0 KiB
JavaScript

'use strict';
const common = require('../common.js');
const fs = require('fs');
const tmpdir = require('../../test/common/tmpdir');
tmpdir.refresh();
// Some variants are commented out as they do not show a change and just slow
const bench = common.createBenchmark(main, {
encoding: ['utf8'],
useFd: ['true', 'false'],
length: [1024, 102400, 1024 * 1024],
// useBuffer: ['true', 'false'],
useBuffer: ['false'],
// func: ['appendFile', 'writeFile'],
func: ['writeFile'],
n: [1e3],
});
function main({ n, func, encoding, length, useFd, useBuffer }) {
tmpdir.refresh();
const enc = encoding === 'undefined' ? undefined : encoding;
const path = tmpdir.resolve(`.writefilesync-file-${Date.now()}`);
useFd = useFd === 'true';
const file = useFd ? fs.openSync(path, 'w') : path;
let data = 'a'.repeat(length);
if (useBuffer === 'true') data = Buffer.from(data, encoding);
const fn = fs[func + 'Sync'];
bench.start();
for (let i = 0; i < n; ++i) {
fn(file, data, enc);
}
bench.end(n);
if (useFd) fs.closeSync(file);
}