• 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    // Performs `n` single inflateSync operations
29    case 'inflateSync':
30      const inflateSync = zlib.inflateSync;
31      bench.start();
32      for (; i < n; ++i)
33        inflateSync(chunk);
34      bench.end(n);
35      break;
36    default:
37      throw new Error('Unsupported inflate method');
38  }
39}
40