var size = 0xFFFF;
var buf = new ArrayBuffer(size);
var view = new DataView(buf);
var byte = new Uint8Array(buf);
var data = new Uint8Array(size);
for (let i = 0; i < size; i++) {
data[i] = 100*Math.random();
}
function copy(dst, src, size) {
for (let i=0;i<size;i++)dst[i]=src[i];
}
for (let i; i < size; i++) {
view.setUint8(i, data[i]);
}
for (let i; i < size; i++) {
byte[i] = data[i];
}
byte.set(data);
(new Float64Array(buf, 0, size/8))
.set(new Float64Array(data.buffer, 0, size/8));
copy(byte, data, data.length)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
DataView | |
Uint8Array by byte | |
Uint8Array with set | |
with Float64Array | |
function copy |
Test name | Executions per second |
---|---|
DataView | 13752860.0 Ops/sec |
Uint8Array by byte | 13879972.0 Ops/sec |
Uint8Array with set | 859023.6 Ops/sec |
with Float64Array | 584556.6 Ops/sec |
function copy | 21093.6 Ops/sec |
Let's break down the provided benchmark and its test cases.
Benchmark Overview
The benchmark is designed to compare different approaches for copying data from an ArrayBuffer into a Uint8Array. The test case uses a JavaScript ArrayBuffer, which is a mutable, typed view of a binary buffer in memory.
Test Cases
There are four main test cases:
set()
method provided by the Uint8Array class to set its elements.Library: DataView
The DataView API is used in the first two test cases (DataView and Uint8Array by byte). The DataView API provides a way to view a binary buffer as a sequence of numbers, where each number has at most four bytes. In this case, it's used to set each byte of the Uint8Array.
Pros and Cons
Uint8Array with set
The set()
method provided by the Uint8Array class is used in this test case. This method is a more modern and efficient way to set the elements of an array.
Pros: provides a simpler and more readable syntax than manual indexing. Cons: may be slower than other methods due to its overhead.
Float64Array
The Float64Array class is created in this test case, but it's not used to copy data from the ArrayBuffer. Instead, another Float64Array is passed as an argument to set its elements.
Pros: provides a convenient way to create a new array and set its elements. Cons: may be slower than other methods due to its overhead.
Function copy
This test case uses a custom copy()
function to assign each byte of the Uint8Array. This approach is often used for performance-critical code, but it can also lead to errors if not implemented correctly.
Pros: provides fine-grained control over the copying process. Cons: requires manual indexing and error handling.
Other Alternatives
There are other ways to copy data from an ArrayBuffer into a Uint8Array, such as using the subarray()
method or creating a new array using the spread operator ([...])
. However, these methods may not be as efficient or readable as the ones used in the benchmark.
In summary, this benchmark compares different approaches for copying data from an ArrayBuffer into a Uint8Array. The test cases demonstrate the performance differences between using DataView, manual indexing, and other methods like set()
or a custom function.