aboutsummaryrefslogtreecommitdiff
path: root/benchmark/buffers/buffer-read-float.js
blob: 5dda2486c6711aa2418fa0b1baee7a273f22761a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict';
const common = require('../common.js');

const bench = common.createBenchmark(main, {
  noAssert: ['false', 'true'],
  type: ['Double', 'Float'],
  endian: ['BE', 'LE'],
  value: ['zero', 'big', 'small', 'inf', 'nan'],
  millions: [1]
});

function main(conf) {
  const noAssert = conf.noAssert === 'true';
  const len = +conf.millions * 1e6;
  const buff = Buffer.alloc(8);
  const type = conf.type || 'Double';
  const endian = conf.endian;
  const fn = `read${type}${endian}`;
  const values = {
    Double: {
      zero: 0,
      big: 2 ** 1023,
      small: 2 ** -1074,
      inf: Infinity,
      nan: NaN,
    },
    Float: {
      zero: 0,
      big: 2 ** 127,
      small: 2 ** -149,
      inf: Infinity,
      nan: NaN,
    },
  };
  const value = values[type][conf.value];

  buff[`write${type}${endian}`](value, 0, noAssert);
  const testFunction = new Function('buff', `
    for (var i = 0; i !== ${len}; i++) {
      buff.${fn}(0, ${JSON.stringify(noAssert)});
    }
  `);
  bench.start();
  testFunction(buff);
  bench.end(len / 1e6);
}