• 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: ['inflate', 'inflateSync'],
7  inputLen: [1024],
8  n: [4e5],
9});
10
11function main({ n, method, inputLen }) {
12  // Default method value for tests.
13  method = method || 'inflate';
14  const chunk = zlib.deflateSync(Buffer.alloc(inputLen, 'a'));
15
16  let i = 0;
17  switch (method) {
18    // Performs `n` single inflate operations
19    case 'inflate': {
20      const inflate = zlib.inflate;
21      bench.start();
22      (function next(err, result) {
23        if (i++ === n)
24          return bench.end(n);
25        inflate(chunk, next);
26      })();
27      break;
28    }
29    // Performs `n` single inflateSync operations
30    case 'inflateSync': {
31      const inflateSync = zlib.inflateSync;
32      bench.start();
33      for (; i < n; ++i)
34        inflateSync(chunk);
35      bench.end(n);
36      break;
37    }
38    default:
39      throw new Error('Unsupported inflate method');
40  }
41}
42