benchmark: add zstdCompress benchmark

Signed-off-by: James Ross <james@jross.me>
PR-URL: https://github.com/nodejs/node/pull/66358
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
This commit is contained in:
James Ross authored and Antoine du Hamel committed 2026-10-03 17:30:21 +02:00
1 parent 9b350d80ff
commit 168b764fec
1 file changed
+41
+41
View File
@@ -0,0 +1,41 @@
'use strict';
const common = require('../common.js');
const fs = require('fs');
const zlib = require('zlib');
const bench = common.createBenchmark(main, {
method: ['zstdCompress', 'zstdCompressSync'],
level: [3, 12],
inputLen: [16 * 1024, 256 * 1024],
n: [100],
});
function main({ n, method, level, inputLen }) {
const input = Buffer.alloc(inputLen, fs.readFileSync(__filename));
const opts = { params: { [zlib.constants.ZSTD_c_compressionLevel]: level } };
switch (method) {
// Performs `n` single zstdCompress operations
case 'zstdCompress': {
let i = 0;
bench.start();
(function next(err) {
if (err) throw err;
if (i++ === n)
return bench.end(n);
zlib.zstdCompress(input, opts, next);
})();
break;
}
// Performs `n` single zstdCompressSync operations
case 'zstdCompressSync': {
bench.start();
for (let i = 0; i < n; ++i)
zlib.zstdCompressSync(input, opts);
bench.end(n);
break;
}
default:
throw new Error('Unsupported zstd method');
}
}