var size = 8 * 40;
var srcBuf8 = new ArrayBuffer(size);
var srcBufPtr8 = new Uint8Array(srcBuf8);
var srcBuf16 = new ArrayBuffer(size*2);
var srcBufPtr16 = new Uint16Array(srcBuf16);
var srcBuf32 = new ArrayBuffer(size*4);
var srcBufPtr32 = new Uint32Array(srcBuf32);
var srcBuf64 = new ArrayBuffer(size*8);
var srcBufPtr64 = new BigUint64Array(srcBuf64);
for (let i = 0; i < size; i++) {
srcBufPtr8[i] = 100 * Math.random();
}
for (let i = 0; i < size; i++) {
srcBufPtr16[i] = srcBufPtr8[i];
}
for (let i = 0; i < size; i++) {
srcBufPtr32[i] = srcBufPtr8[i];
}
for (let i = 0; i < size; i++) {
srcBufPtr64[i] = BigInt(srcBufPtr8[i]);
}
let smallerCount = 0;
for (let i = 0; i < (size-1); i++) {
if (srcBufPtr32[i] < srcBufPtr32[i+1]) {
smallerCount++;
}
}
let smallerCount = 0;
for (let i = 0; i < (size-1); i++) {
if (srcBufPtr64[i] < srcBufPtr64[i+1]) {
smallerCount++;
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Uint32Array | |
Biguint64array |
Test name | Executions per second |
---|---|
Uint32Array | 14701.2 Ops/sec |
Biguint64array | 12380.2 Ops/sec |
The benchmark you've provided tests the performance of two different typed arrays in JavaScript: Uint32Array
and BigUint64Array
. The purpose of this benchmark is to compare the speed of sequential access and comparison operations between these two data structures, specifically how many times it can execute a counting operation where it checks if the previous element is smaller than the next one.
Uint32Array
:
Uint32Array
.BigUint64Array
:
Uint32Array
.BigUint64Array
.Uint32Array
:
BigUint64Array
in scenarios where the data fits within its limits.BigUint64Array
:
Uint32Array
(4 bytes per entry).Uint32Array
uses less memory than BigUint64Array
, it may be preferred in scenarios where a large array is needed, and data fits the 32-bit unsigned integer requirement.Uint32Array
is sufficient. In contrast, if integer math or representation of very large values is needed, BigUint64Array
is beneficial despite potential performance trade-offs.Apart from Uint32Array
and BigUint64Array
, there are several other typed arrays available in JavaScript:
Int32Array
: This is similar to Uint32Array
but allows signed integers (positive and negative).Float32Array
/ Float64Array
: Useful for applications requiring floating-point computations.Int8Array
, Uint8Array
, Uint16Array
, and specialized arrays for different data types depending on the need.This benchmark effectively illustrates the performance differences between two important data structures in JavaScript, allowing developers to make informed choices based on their specific requirements regarding performance, memory usage, and value range. Understanding the strengths and limitations of these typed arrays can be essential for optimizing performance in JavaScript applications.