2016-02-19 17:03:16 -08:00
|
|
|
'use strict';
|
2017-09-13 22:48:53 -03:00
|
|
|
const common = require('../common.js');
|
2025-04-24 07:50:22 -07:00
|
|
|
const { Buffer } = require('buffer');
|
2017-09-13 22:48:53 -03:00
|
|
|
const types = [
|
2018-06-03 20:30:46 +03:00
|
|
|
'BigUInt64LE',
|
|
|
|
'BigUInt64BE',
|
|
|
|
'BigInt64LE',
|
|
|
|
'BigInt64BE',
|
2016-11-24 11:43:35 -08:00
|
|
|
'UInt8',
|
|
|
|
'UInt16LE',
|
|
|
|
'UInt16BE',
|
|
|
|
'UInt32LE',
|
|
|
|
'UInt32BE',
|
2018-01-30 21:06:59 +01:00
|
|
|
'UIntLE',
|
|
|
|
'UIntBE',
|
2016-11-24 11:43:35 -08:00
|
|
|
'Int8',
|
|
|
|
'Int16LE',
|
|
|
|
'Int16BE',
|
|
|
|
'Int32LE',
|
|
|
|
'Int32BE',
|
2018-01-30 21:06:59 +01:00
|
|
|
'IntLE',
|
|
|
|
'IntBE',
|
2016-11-24 11:43:35 -08:00
|
|
|
'FloatLE',
|
|
|
|
'FloatBE',
|
|
|
|
'DoubleLE',
|
2019-02-04 22:06:08 -08:00
|
|
|
'DoubleBE',
|
2016-11-24 11:43:35 -08:00
|
|
|
];
|
|
|
|
|
2017-09-13 22:48:53 -03:00
|
|
|
const bench = common.createBenchmark(main, {
|
2019-03-04 01:34:00 +01:00
|
|
|
buffer: ['fast'],
|
2016-11-24 11:43:35 -08:00
|
|
|
type: types,
|
2023-02-04 19:19:25 +01:00
|
|
|
n: [1e6],
|
2013-02-11 23:53:27 -08:00
|
|
|
});
|
|
|
|
|
2016-05-08 23:04:17 -07:00
|
|
|
const INT8 = 0x7f;
|
|
|
|
const INT16 = 0x7fff;
|
|
|
|
const INT32 = 0x7fffffff;
|
2018-01-30 21:06:59 +01:00
|
|
|
const INT48 = 0x7fffffffffff;
|
2018-06-03 20:30:46 +03:00
|
|
|
const INT64 = 0x7fffffffffffffffn;
|
2018-01-30 21:06:59 +01:00
|
|
|
const UINT8 = 0xff;
|
|
|
|
const UINT16 = 0xffff;
|
2018-03-11 23:21:17 +01:00
|
|
|
const UINT32 = 0xffffffff;
|
2018-06-03 20:30:46 +03:00
|
|
|
const UINT64 = 0xffffffffffffffffn;
|
2013-02-11 23:53:27 -08:00
|
|
|
|
2017-09-13 22:48:53 -03:00
|
|
|
const mod = {
|
2018-06-03 20:30:46 +03:00
|
|
|
writeBigInt64BE: INT64,
|
|
|
|
writeBigInt64LE: INT64,
|
|
|
|
writeBigUInt64BE: UINT64,
|
|
|
|
writeBigUInt64LE: UINT64,
|
2013-02-11 23:53:27 -08:00
|
|
|
writeInt8: INT8,
|
|
|
|
writeInt16BE: INT16,
|
|
|
|
writeInt16LE: INT16,
|
|
|
|
writeInt32BE: INT32,
|
|
|
|
writeInt32LE: INT32,
|
|
|
|
writeUInt8: UINT8,
|
|
|
|
writeUInt16BE: UINT16,
|
|
|
|
writeUInt16LE: UINT16,
|
2018-03-11 23:21:17 +01:00
|
|
|
writeUInt32BE: UINT32,
|
|
|
|
writeUInt32LE: UINT32,
|
2018-01-30 21:06:59 +01:00
|
|
|
writeUIntLE: INT8,
|
|
|
|
writeUIntBE: INT16,
|
|
|
|
writeIntLE: INT32,
|
2023-02-04 19:19:25 +01:00
|
|
|
writeIntBE: INT48,
|
2013-02-11 23:53:27 -08:00
|
|
|
};
|
|
|
|
|
2018-01-30 21:06:59 +01:00
|
|
|
const byteLength = {
|
|
|
|
writeUIntLE: 1,
|
|
|
|
writeUIntBE: 2,
|
|
|
|
writeIntLE: 4,
|
2023-02-04 19:19:25 +01:00
|
|
|
writeIntBE: 6,
|
2018-01-30 21:06:59 +01:00
|
|
|
};
|
|
|
|
|
2018-03-17 20:49:09 +05:30
|
|
|
function main({ n, buf, type }) {
|
2019-03-04 01:34:00 +01:00
|
|
|
const buff = buf === 'fast' ?
|
|
|
|
Buffer.alloc(8) :
|
2025-04-24 07:50:22 -07:00
|
|
|
Buffer.allocUnsafeSlow(8);
|
2020-02-12 21:02:35 +01:00
|
|
|
const fn = `write${type}`;
|
2013-02-11 23:53:27 -08:00
|
|
|
|
2018-01-30 21:06:59 +01:00
|
|
|
if (!/\d/.test(fn))
|
2018-03-17 20:49:09 +05:30
|
|
|
benchSpecialInt(buff, fn, n);
|
2018-06-03 20:30:46 +03:00
|
|
|
else if (/BigU?Int/.test(fn))
|
|
|
|
benchBigInt(buff, fn, BigInt(n));
|
2018-01-30 21:06:59 +01:00
|
|
|
else if (/Int/.test(fn))
|
2018-03-17 20:49:09 +05:30
|
|
|
benchInt(buff, fn, n);
|
2013-02-11 23:53:27 -08:00
|
|
|
else
|
2018-03-17 20:49:09 +05:30
|
|
|
benchFloat(buff, fn, n);
|
2018-01-30 21:06:59 +01:00
|
|
|
}
|
|
|
|
|
2018-06-03 20:30:46 +03:00
|
|
|
function benchBigInt(buff, fn, n) {
|
|
|
|
const m = mod[fn];
|
|
|
|
bench.start();
|
2019-07-26 10:54:19 -05:00
|
|
|
for (let i = 0n; i !== n; i++) {
|
2018-06-03 20:30:46 +03:00
|
|
|
buff[fn](i & m, 0);
|
|
|
|
}
|
|
|
|
bench.end(Number(n));
|
|
|
|
}
|
|
|
|
|
2018-03-17 20:49:09 +05:30
|
|
|
function benchInt(buff, fn, n) {
|
2018-01-30 21:06:59 +01:00
|
|
|
const m = mod[fn];
|
|
|
|
bench.start();
|
2019-07-26 10:54:19 -05:00
|
|
|
for (let i = 0; i !== n; i++) {
|
2018-01-30 21:06:59 +01:00
|
|
|
buff[fn](i & m, 0);
|
|
|
|
}
|
2018-03-17 20:49:09 +05:30
|
|
|
bench.end(n);
|
2013-02-11 23:53:27 -08:00
|
|
|
}
|
|
|
|
|
2018-03-17 20:49:09 +05:30
|
|
|
function benchSpecialInt(buff, fn, n) {
|
2017-09-13 22:48:53 -03:00
|
|
|
const m = mod[fn];
|
2018-01-30 21:06:59 +01:00
|
|
|
const byte = byteLength[fn];
|
2013-02-11 23:53:27 -08:00
|
|
|
bench.start();
|
2019-07-26 10:54:19 -05:00
|
|
|
for (let i = 0; i !== n; i++) {
|
2018-01-30 21:06:59 +01:00
|
|
|
buff[fn](i & m, 0, byte);
|
2018-01-23 13:17:39 +01:00
|
|
|
}
|
2018-03-17 20:49:09 +05:30
|
|
|
bench.end(n);
|
2013-02-11 23:53:27 -08:00
|
|
|
}
|
|
|
|
|
2018-03-17 20:49:09 +05:30
|
|
|
function benchFloat(buff, fn, n) {
|
2013-02-11 23:53:27 -08:00
|
|
|
bench.start();
|
2019-07-26 10:54:19 -05:00
|
|
|
for (let i = 0; i !== n; i++) {
|
2018-01-30 21:06:59 +01:00
|
|
|
buff[fn](i, 0);
|
2018-01-23 13:17:39 +01:00
|
|
|
}
|
2018-03-17 20:49:09 +05:30
|
|
|
bench.end(n);
|
2013-02-11 23:53:27 -08:00
|
|
|
}
|