<!--your preparation HTML code goes here-->
function bigIntToBigEndianBytesLoopParseInt(value) {
const hex = value.toString(16).padStart(16, '0');
const bytes = new Uint8Array(8);
for (let i = 0; i < 8; i++) {
bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
}
return bytes;
}
function bigIntToBigEndianBytesLoopConstructor(value) {
const hex = value.toString(16).padStart(16, '0');
const bytes = new Uint8Array(8);
for (let i = 0; i < 8; i++) {
bytes[i] = Number(hex.slice(i * 2, i * 2 + 2), 16);
}
return bytes;
}
function bigIntToBigEndianBytesDataView(value) {
const buf = new ArrayBuffer(8);
const view = new DataView(buf);
view.setBigUint64(0, value);
return new Uint8Array(buf);
}
function bigIntToUint8ArrayDirectConstructor(value) {
const result = new Uint8Array(8);
result[7] = Number(value & 0xffn);
result[6] = Number((value >> 8n) & 0xffn);
result[5] = Number((value >> 16n) & 0xffn);
result[4] = Number((value >> 24n) & 0xffn );
result[3] = Number((value >> 32n) & 0xffn);
result[2] = Number((value >> 40n) & 0xffn);
result[1] = Number((value >> 48n) & 0xffn);
result[0] = Number((value >> 56n) & 0xffn);
return result;
}
function bigIntToUint8ArrayDirectParseInt(value) {
const result = new Uint8Array(8);
result[7] = parseInt(value & 0xffn);
result[6] = parseInt((value >> 8n) & 0xffn);
result[5] = parseInt((value >> 16n) & 0xffn);
result[4] = parseInt((value >> 24n) & 0xffn );
result[3] = parseInt((value >> 32n) & 0xffn);
result[2] = parseInt((value >> 40n) & 0xffn);
result[1] = parseInt((value >> 48n) & 0xffn);
result[0] = parseInt((value >> 56n) & 0xffn);
return result;
}
bigIntToBigEndianBytesLoopParseInt(32149814014n)
bigIntToBigEndianBytesLoopConstructor(32149814014n)
bigIntToBigEndianBytesDataView(32149814014n)
bigIntToUint8ArrayDirectConstructor(32149814014n)
bigIntToUint8ArrayDirectParseInt(32149814014n)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Loop ParseInt | |
Loop Constructor | |
DataView | |
Direct Constructor | |
Direct ParseInt |
Test name | Executions per second |
---|---|
Loop ParseInt | 2849222.8 Ops/sec |
Loop Constructor | 1508101.4 Ops/sec |
DataView | 2983838.0 Ops/sec |
Direct Constructor | 5215165.5 Ops/sec |
Direct ParseInt | 2909482.8 Ops/sec |
In this benchmark, the goal is to evaluate the performance of different methods for converting a BigInt value to an array of bytes in big-endian format. The benchmark provides five different implementations, each employing distinct techniques for performing the conversion. Let's break down each approach:
Loop ParseInt Method (bigIntToBigEndianBytesLoopParseInt
)
parseInt
.parseInt
, especially when executed multiple times in loops.Loop Constructor Method (bigIntToBigEndianBytesLoopConstructor
)
parseInt
, it uses the Number
constructor to convert hex pairs to bytes.Number
can be slightly more efficient than parseInt
, as there's no string parsing involved.DataView Method (bigIntToBigEndianBytesDataView
)
DataView
class, which provides a low-level interface for reading and writing buffer data, to set a BigInt directly into an ArrayBuffer and obtain the Uint8Array representation.ArrayBuffer
and DataView
.Direct Constructor Method (bigIntToUint8ArrayDirectConstructor
)
Direct ParseInt Method (bigIntToUint8ArrayDirectParseInt
)
parseInt
to convert isolated portions into bytes.parseInt
.parseInt
, which can make it slower than the pure bit manipulation approach.Based on the results reported in the latest benchmark, the Direct Constructor
method outperformed all other methods, registering over 5.2 million executions per second. In contrast, the Loop Constructor
method was the slowest, with approximately 1.5 million executions per second.
Buffer
class may provide another means of performing similar conversions, especially when working with binary data.TypedArray
interfaces can also be considered for different data handling needs, like managing larger binary data.big-integer
or decimal.js
offer BigInt-like functionality and may include utility features for conversion, though they may not necessarily outperform these tailored implementations.This benchmark effectively illustrates how various methodologies can be harnessed for converting BigInts to byte arrays, with trade-offs in complexity, performance, and clarity.