1'use strict';
2const common = require('../common.js');
3
4const types = [
5  'Uint8',
6  'Uint16LE',
7  'Uint16BE',
8  'Uint32LE',
9  'Uint32BE',
10  'Int8',
11  'Int16LE',
12  'Int16BE',
13  'Int32LE',
14  'Int32BE',
15  'Float32LE',
16  'Float32BE',
17  'Float64LE',
18  'Float64BE',
19];
20
21const bench = common.createBenchmark(main, {
22  type: types,
23  n: [1e6],
24});
25
26const INT8 = 0x7f;
27const INT16 = 0x7fff;
28const INT32 = 0x7fffffff;
29const UINT8 = INT8 * 2;
30const UINT16 = INT16 * 2;
31const UINT32 = INT32 * 2;
32
33const mod = {
34  setInt8: INT8,
35  setInt16: INT16,
36  setInt32: INT32,
37  setUint8: UINT8,
38  setUint16: UINT16,
39  setUint32: UINT32,
40};
41
42function main({ n, type }) {
43  const ab = new ArrayBuffer(8);
44  const dv = new DataView(ab, 0, 8);
45  const le = /LE$/.test(type);
46  const fn = `set${type.replace(/[LB]E$/, '')}`;
47
48  if (/int/i.test(fn))
49    benchInt(dv, fn, n, le);
50  else
51    benchFloat(dv, fn, n, le);
52}
53
54function benchInt(dv, fn, len, le) {
55  const m = mod[fn];
56  const method = dv[fn];
57  bench.start();
58  for (let i = 0; i < len; i++) {
59    method.call(dv, 0, i % m, le);
60  }
61  bench.end(len);
62}
63
64function benchFloat(dv, fn, len, le) {
65  const method = dv[fn];
66  bench.start();
67  for (let i = 0; i < len; i++) {
68    method.call(dv, 0, i * 0.1, le);
69  }
70  bench.end(len);
71}
72