summaryrefslogtreecommitdiff
path: root/benchmark/buffers/buffer-read-float.js
blob: e8c4f8bf54962878b2edd617d0a3efccb45815a1 (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
'use strict';
const common = require('../common.js');

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

function main({ n, type, endian, value }) {
  type = type || 'Double';
  const buff = Buffer.alloc(8);
  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,
    },
  };

  buff[`write${type}${endian}`](values[type][value], 0);

  bench.start();
  for (let i = 0; i !== n; i++) {
    buff[fn](0);
  }
  bench.end(n);
}