var last = 100000;
for (let i; i < last; i++) {
const x = new ArrayBuffer(4);
const y = new Uint32Array(x);
y[0] = 255;
}
for (let i; i < last; i++) {
var y = 255;
}
const x = new ArrayBuffer(4 * last);
const y = new Uint32Array(x);
last = y.length;
for (let i; i < last; i++) {
y[0] = 255;
}
const x = new ArrayBuffer(last);
const y = new Uint8Array(x);
for (let i; i < last; i++) {
y[0] = 255;
}
for (let i; i < last; i++) {
const x = new ArrayBuffer(1);
const y = new Uint8Array(x);
y[0] = 255;
}
for (let i; i < last; i++) {
var y = Number(255);
}
for (let i; i < last; i++) {
var y = new Number(255);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Uint32 | |
Normal | |
Uint32Array | |
Uint8Array | |
Uint8Array (maybe faster due to garbage collector) | |
Number | |
Instantiation Number |
Test name | Executions per second |
---|---|
Uint32 | 85373024.0 Ops/sec |
Normal | 85390544.0 Ops/sec |
Uint32Array | 34818.4 Ops/sec |
Uint8Array | 106830.5 Ops/sec |
Uint8Array (maybe faster due to garbage collector) | 82938608.0 Ops/sec |
Number | 83280696.0 Ops/sec |
Instantiation Number | 82855008.0 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
Benchmark Description
The benchmark is testing different ways to create and use arrays for storing numbers in JavaScript. Specifically, it compares:
ArrayBuffer
(with Uint32Array
, Uint8Array
, or Uint8Array
with a small size)var y = 255;
)Number
typeNumber
(not used in this benchmark)Options Compared
The options being compared are:
Uint8Array
: an array buffer with a single byte, potentially benefiting from garbage collection.Uint32Array
, Uint8Array
with larger size: an array buffer for storing 32-bit or 8-bit numbers directly.var y = 255;
): creating a variable without initializing it.Pros and Cons
Here's a brief overview of the pros and cons of each option:
Uint32Array
.Uint8Array
with larger size: Pros:Test Case Analysis
The benchmark tests the execution speed of each option. The test cases are designed to create arrays or variables with a large number of elements (100,000), and then iterate over them to set their values.
For Uint8Array
(with small size), Chrome 104 is slightly faster, likely due to garbage collection benefits.
For Number
, Chrome 104 is slower than the other options, likely because it involves more overhead when creating a new variable.
Library and Special JS Features
Alternatives
If you want to explore alternatives, here are some options:
Uint8Array
, Uint32Array
, or Number
, consider native array types like Float64Array
or BigInt64Array
. These may offer better performance and precision.Float64Array
, Int8Array
). This might be an alternative, but it's not directly comparable to the options in this benchmark.Keep in mind that these alternatives might require additional setup and understanding of their behavior.