• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common.js');
3const zlib = require('zlib');
4
5const bench = common.createBenchmark(main, {
6  method: ['createDeflate', 'deflate', 'deflateSync'],
7  inputLen: [1024],
8  n: [4e5]
9});
10
11function main({ n, method, inputLen }) {
12  // Default method value for testing.
13  method = method || 'deflate';
14  const chunk = Buffer.alloc(inputLen, 'a');
15
16  switch (method) {
17    // Performs `n` writes for a single deflate stream
18    case 'createDeflate': {
19      let i = 0;
20      const deflater = zlib.createDeflate();
21      deflater.resume();
22      deflater.on('finish', () => {
23        bench.end(n);
24      });
25
26      bench.start();
27      (function next() {
28        if (i++ === n)
29          return deflater.end();
30        deflater.write(chunk, next);
31      })();
32      break;
33    }
34    // Performs `n` single deflate operations
35    case 'deflate': {
36      let i = 0;
37      const deflate = zlib.deflate;
38      bench.start();
39      (function next(err, result) {
40        if (i++ === n)
41          return bench.end(n);
42        deflate(chunk, next);
43      })();
44      break;
45    }
46    // Performs `n` single deflateSync operations
47    case 'deflateSync': {
48      const deflateSync = zlib.deflateSync;
49      bench.start();
50      for (let i = 0; i < n; ++i)
51        deflateSync(chunk);
52      bench.end(n);
53      break;
54    }
55    default:
56      throw new Error('Unsupported deflate method');
57  }
58}
59