• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common.js');
4const assert = require('assert');
5const bench = common.createBenchmark(main, {
6  type: [
7    'fast-alloc',
8    'fast-alloc-fill',
9    'fast-allocUnsafe',
10    'slow-allocUnsafe',
11  ],
12  len: [10, 1024, 4096, 8192],
13  n: [6e5]
14});
15
16function main({ len, n, type }) {
17  let fn, i;
18  switch (type) {
19    case 'fast-alloc':
20      fn = Buffer.alloc;
21      break;
22    case 'fast-alloc-fill':
23      bench.start();
24      for (i = 0; i < n; i++) {
25        Buffer.alloc(len, 0);
26      }
27      bench.end(n);
28      return;
29    case 'fast-allocUnsafe':
30      fn = Buffer.allocUnsafe;
31      break;
32    case 'slow-allocUnsafe':
33      fn = Buffer.allocUnsafeSlow;
34      break;
35    default:
36      assert.fail('Should not get here');
37  }
38
39  bench.start();
40  for (i = 0; i < n; i++) {
41    fn(len);
42  }
43  bench.end(n);
44}
45