• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common.js');
3
4const types = [
5  'BigUInt64LE',
6  'BigUInt64BE',
7  'BigInt64LE',
8  'BigInt64BE',
9  'UInt8',
10  'UInt16LE',
11  'UInt16BE',
12  'UInt32LE',
13  'UInt32BE',
14  'UIntLE',
15  'UIntBE',
16  'Int8',
17  'Int16LE',
18  'Int16BE',
19  'Int32LE',
20  'Int32BE',
21  'IntLE',
22  'IntBE',
23  'FloatLE',
24  'FloatBE',
25  'DoubleLE',
26  'DoubleBE',
27];
28
29const bench = common.createBenchmark(main, {
30  buffer: ['fast'],
31  type: types,
32  n: [1e6],
33});
34
35const INT8 = 0x7f;
36const INT16 = 0x7fff;
37const INT32 = 0x7fffffff;
38const INT48 = 0x7fffffffffff;
39const INT64 = 0x7fffffffffffffffn;
40const UINT8 = 0xff;
41const UINT16 = 0xffff;
42const UINT32 = 0xffffffff;
43const UINT64 = 0xffffffffffffffffn;
44
45const mod = {
46  writeBigInt64BE: INT64,
47  writeBigInt64LE: INT64,
48  writeBigUInt64BE: UINT64,
49  writeBigUInt64LE: UINT64,
50  writeInt8: INT8,
51  writeInt16BE: INT16,
52  writeInt16LE: INT16,
53  writeInt32BE: INT32,
54  writeInt32LE: INT32,
55  writeUInt8: UINT8,
56  writeUInt16BE: UINT16,
57  writeUInt16LE: UINT16,
58  writeUInt32BE: UINT32,
59  writeUInt32LE: UINT32,
60  writeUIntLE: INT8,
61  writeUIntBE: INT16,
62  writeIntLE: INT32,
63  writeIntBE: INT48,
64};
65
66const byteLength = {
67  writeUIntLE: 1,
68  writeUIntBE: 2,
69  writeIntLE: 4,
70  writeIntBE: 6,
71};
72
73function main({ n, buf, type }) {
74  const buff = buf === 'fast' ?
75    Buffer.alloc(8) :
76    require('buffer').SlowBuffer(8);
77  const fn = `write${type}`;
78
79  if (!/\d/.test(fn))
80    benchSpecialInt(buff, fn, n);
81  else if (/BigU?Int/.test(fn))
82    benchBigInt(buff, fn, BigInt(n));
83  else if (/Int/.test(fn))
84    benchInt(buff, fn, n);
85  else
86    benchFloat(buff, fn, n);
87}
88
89function benchBigInt(buff, fn, n) {
90  const m = mod[fn];
91  bench.start();
92  for (let i = 0n; i !== n; i++) {
93    buff[fn](i & m, 0);
94  }
95  bench.end(Number(n));
96}
97
98function benchInt(buff, fn, n) {
99  const m = mod[fn];
100  bench.start();
101  for (let i = 0; i !== n; i++) {
102    buff[fn](i & m, 0);
103  }
104  bench.end(n);
105}
106
107function benchSpecialInt(buff, fn, n) {
108  const m = mod[fn];
109  const byte = byteLength[fn];
110  bench.start();
111  for (let i = 0; i !== n; i++) {
112    buff[fn](i & m, 0, byte);
113  }
114  bench.end(n);
115}
116
117function benchFloat(buff, fn, n) {
118  bench.start();
119  for (let i = 0; i !== n; i++) {
120    buff[fn](i, 0);
121  }
122  bench.end(n);
123}
124