function numberToUint8Array64LE_DataView(num) {
const buf = new ArrayBuffer(8)
const view = new DataView(buf)
view.setUint32(0, num, true)
view.setUint32(4, num >>> 32, true)
return new Uint8Array(buf)
}
function numberToUint8Array64LE_direct(num) {
const result = new Uint8Array(8)
result[0] = num & 0xff
result[1] = (num >>> 8) & 0xff
result[2] = (num >>> 16) & 0xff
result[3] = (num >>> 24) & 0xff
result[4] = (num >>> 32) & 0xff
result[5] = (num >>> 40) & 0xff
result[6] = (num >>> 48) & 0xff
result[7] = (num >>> 56) & 0xff
return result
}
function numberToUint8Array64LE_bigint(num) {
const bigIntValue = BigInt.asUintN(64, BigInt(num));
const buffer = new ArrayBuffer(8);
const dataView = new DataView(buffer);
dataView.setBigUint64(0, bigIntValue, true);
return Uint8Array.from(buffer.slice(0, 8));
}
numberToUint8Array64LE_DataView(0xababababab)
numberToUint8Array64LE_direct(0xababababab)
numberToUint8Array64LE_bigint(0xababababab)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Through DataView | |
Direct | |
With BigInt |
Test name | Executions per second |
---|---|
Through DataView | 4081559.0 Ops/sec |
Direct | 41327908.0 Ops/sec |
With BigInt | 1759107.8 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition JSON
The provided JSON represents a benchmark that tests the performance of converting a 64-bit little-endian signed integer (num
) to a Uint8Array
using three different approaches:
numberToUint8Array64LE_DataView
: This approach uses the DataView
API to create a buffer and manipulate its bytes directly.numberToUint8Array64LE_direct
: This approach manually extracts individual bytes from the input integer using bitwise operations.numberToUint8Array64LE_bigint
: This approach converts the input integer to a 64-bit BigInt
value, then uses the DataView
API to create a buffer and manipulate its bytes.Options Compared
The three approaches differ in their use of low-level memory manipulation:
numberToUint8Array64LE_DataView
relies on the DataView
API to manage the conversion process.numberToUint8Array64LE_direct
uses bitwise operations to extract individual bytes from the input integer.numberToUint8Array64LE_bigint
converts the input integer to a BigInt
value, which is then converted to a buffer using the DataView
API.Pros and Cons of Each Approach
Here's a brief summary:
numberToUint8Array64LE_DataView
:DataView
object and managing its bytes.numberToUint8Array64LE_direct
:numberToUint8Array64LE_bigint
:BigInt
values, potentially faster due to optimized native code for BigInt
.BigInt
object.Library
In this benchmark, no libraries are explicitly mentioned. However, it's worth noting that some browsers, like Firefox, may use various internal APIs or libraries to optimize performance-critical code.
Special JS Features
The benchmark uses some special JavaScript features:
BigInt
: Introduced in ECMAScript 2019, BigInt
allows representing integers with arbitrary precision.DataView
: Introduced in ECMAScript 5, DataView
provides a low-level API for manipulating buffers and bytes.These features are used to create a buffer and manipulate its bytes using different approaches.
Other Alternatives
If you're looking for alternatives or optimizations for this benchmark, consider the following:
TypedArray
instead of Uint8Array
: If performance is critical, using a typed array like Int32Array
or Float64Array
might be faster than converting to a Uint8Array
.libuv
(for Linux) or nodejs
(for Windows and macOS) might provide better performance.Keep in mind that the best approach will depend on the specifics of your use case, target platform, and desired trade-offs between development time, maintenance overhead, and performance.