Skip to content

Commit b22c547

Browse files
committed
benchmark: add zstdCompress benchmark
Signed-off-by: James Ross <james@jross.me>
1 parent d708148 commit b22c547

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

‎benchmark/zlib/zstd-compress.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const fs = require('fs');
4+
const zlib = require('zlib');
5+
6+
const bench = common.createBenchmark(main, {
7+
method: ['zstdCompress', 'zstdCompressSync'],
8+
level: [3, 12],
9+
inputLen: [16 * 1024, 256 * 1024],
10+
n: [100],
11+
});
12+
13+
function main({ n, method, level, inputLen }) {
14+
const input = Buffer.alloc(inputLen, fs.readFileSync(__filename));
15+
const opts = { params: { [zlib.constants.ZSTD_c_compressionLevel]: level } };
16+
17+
switch (method) {
18+
// Performs `n` single zstdCompress operations
19+
case 'zstdCompress': {
20+
let i = 0;
21+
bench.start();
22+
(function next(err) {
23+
if (err) throw err;
24+
if (i++ === n)
25+
return bench.end(n);
26+
zlib.zstdCompress(input, opts, next);
27+
})();
28+
break;
29+
}
30+
// Performs `n` single zstdCompressSync operations
31+
case 'zstdCompressSync': {
32+
bench.start();
33+
for (let i = 0; i < n; ++i)
34+
zlib.zstdCompressSync(input, opts);
35+
bench.end(n);
36+
break;
37+
}
38+
default:
39+
throw new Error('Unsupported zstd method');
40+
}
41+
}

0 commit comments

Comments
 (0)