mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
ca5f322d32
PR-URL: https://github.com/nodejs/node/pull/46370 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com>
59 lines
1.4 KiB
JavaScript
59 lines
1.4 KiB
JavaScript
'use strict';
|
|
const common = require('../common.js');
|
|
const zlib = require('zlib');
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
method: ['createDeflate', 'deflate', 'deflateSync'],
|
|
inputLen: [1024],
|
|
n: [4e5],
|
|
});
|
|
|
|
function main({ n, method, inputLen }) {
|
|
// Default method value for testing.
|
|
method = method || 'deflate';
|
|
const chunk = Buffer.alloc(inputLen, 'a');
|
|
|
|
switch (method) {
|
|
// Performs `n` writes for a single deflate stream
|
|
case 'createDeflate': {
|
|
let i = 0;
|
|
const deflater = zlib.createDeflate();
|
|
deflater.resume();
|
|
deflater.on('finish', () => {
|
|
bench.end(n);
|
|
});
|
|
|
|
bench.start();
|
|
(function next() {
|
|
if (i++ === n)
|
|
return deflater.end();
|
|
deflater.write(chunk, next);
|
|
})();
|
|
break;
|
|
}
|
|
// Performs `n` single deflate operations
|
|
case 'deflate': {
|
|
let i = 0;
|
|
const deflate = zlib.deflate;
|
|
bench.start();
|
|
(function next(err, result) {
|
|
if (i++ === n)
|
|
return bench.end(n);
|
|
deflate(chunk, next);
|
|
})();
|
|
break;
|
|
}
|
|
// Performs `n` single deflateSync operations
|
|
case 'deflateSync': {
|
|
const deflateSync = zlib.deflateSync;
|
|
bench.start();
|
|
for (let i = 0; i < n; ++i)
|
|
deflateSync(chunk);
|
|
bench.end(n);
|
|
break;
|
|
}
|
|
default:
|
|
throw new Error('Unsupported deflate method');
|
|
}
|
|
}
|