Skip to content

Commit

Permalink
zlib: improve performance
Browse files Browse the repository at this point in the history
PR-URL: #13322
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
mscdex authored and rvagg committed Jun 29, 2017
1 parent 012f60d commit fea2133
Show file tree
Hide file tree
Showing 7 changed files with 482 additions and 360 deletions.
32 changes: 32 additions & 0 deletions benchmark/zlib/creation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
var common = require('../common.js');
var zlib = require('zlib');

var bench = common.createBenchmark(main, {
type: [
'Deflate', 'DeflateRaw', 'Inflate', 'InflateRaw', 'Gzip', 'Gunzip', 'Unzip'
],
options: ['true', 'false'],
n: [5e5]
});

function main(conf) {
var n = +conf.n;
var fn = zlib['create' + conf.type];
if (typeof fn !== 'function')
throw new Error('Invalid zlib type');
var i = 0;

if (conf.options === 'true') {
var opts = {};
bench.start();
for (; i < n; ++i)
fn(opts);
bench.end(n);
} else {
bench.start();
for (; i < n; ++i)
fn();
bench.end(n);
}
}
54 changes: 54 additions & 0 deletions benchmark/zlib/deflate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';
var common = require('../common.js');
var zlib = require('zlib');

var bench = common.createBenchmark(main, {
method: ['createDeflate', 'deflate', 'deflateSync'],
inputLen: [1024],
n: [4e5]
});

function main(conf) {
var n = +conf.n;
var method = conf.method;
var chunk = Buffer.alloc(+conf.inputLen, 'a');

var i = 0;
switch (method) {
// Performs `n` writes for a single deflate stream
case 'createDeflate':
var 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':
var 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':
var deflateSync = zlib.deflateSync;
bench.start();
for (; i < n; ++i)
deflateSync(chunk);
bench.end(n);
break;
default:
throw new Error('Unsupported deflate method');
}
}
Loading

0 comments on commit fea2133

Please sign in to comment.