var size = 0xFF;
var buf = new ArrayBuffer(size);
var view = new DataView(buf);
var byte = new Uint8Array(buf);
var data = [];
for (let i = 0; i < size; i++) {
data[i] = 100*Math.random();
}
for (let i; i < size; i++) {
view.setUint8(i, data[i]);
}
for (let i; i < size; i++) {
byte[i] = data[i];
}
byte.set(data, 0);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
DataView | |
Uint8Array by byte | |
Uint8Array with set |
Test name | Executions per second |
---|---|
DataView | 759847680.0 Ops/sec |
Uint8Array by byte | 776468032.0 Ops/sec |
Uint8Array with set | 5561306.5 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to compare three approaches for setting values in a Uint8Array:
byte[i] = data[i]
).set()
method of the Uint8Array, which sets all elements from an array to the Uint8Array.Comparison
The benchmark aims to measure the performance difference between these three approaches. The test case uses a fixed-size ArrayBuffer (0xFF bytes) and populates it with random values.
Library and Features
No specific libraries or features are used in this benchmark, aside from standard JavaScript APIs.
Options Compared
Here's a brief overview of each approach:
byte[i] = data[i]
). It's a straightforward, language-agnostic way to set values in a Uint8Array.set()
method of the Uint8Array, which sets all elements from an array to the Uint8Array.Other Considerations
When choosing between these approaches, consider the following factors:
Alternatives
If you're looking for alternative approaches, consider:
Keep in mind that the performance differences between these approaches will vary depending on the specific scenario and browser. The benchmark results provided are just a starting point for understanding the relative performance characteristics of each approach.