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();
}
var dataBytes = new Uint8Array(data);
var dataView = new DataView(dataBytes.buffer);
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);
byte.set(dataBytes, 0);
for (let i = 0; i < dataBytes.length; i++) {
view.setUint8(i, dataView.getUint8(i));
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
DataView | |
Uint8Array by byte | |
Uint8Array with set | |
Uint8Array with set other Uint8Array | |
DataView loop |
Test name | Executions per second |
---|---|
DataView | 2188974080.0 Ops/sec |
Uint8Array by byte | 2235617792.0 Ops/sec |
Uint8Array with set | 1328063.2 Ops/sec |
Uint8Array with set other Uint8Array | 49529444.0 Ops/sec |
DataView loop | 9757874.0 Ops/sec |
What is being tested?
The provided JSON represents a benchmark test case that compares the performance of different approaches for setting values in Uint8Array and DataView objects.
Specifically, four test cases are defined:
view.setUint8(i, data[i])
.byte[i] = data[i]
.set
method on a Uint8Array object to set values.set
method on another Uint8Array object to set values.Options compared:
The test compares two main options:
byte[i] = data[i]
(Uint8Array by byte).set
method on a Uint8Array object (Uint8Array with set).Both approaches have their pros and cons:
set
method: More efficient than direct assignment, as it allows the browser's optimized code path to handle the assignment.Other considerations:
The test also compares two variations of the Uint8Array with set approach:
byte.set(dataBytes, 0)
).set
method on the original Uint8Array object (byte.set(data, 0)
).These variations may impact performance due to differences in memory allocation and caching.
Library:
The test uses two built-in JavaScript libraries:
These libraries are essential components of the benchmark test, as they enable the creation and manipulation of binary data in JavaScript.
Special JS feature or syntax:
The test does not use any special JavaScript features or syntax, other than the use of let
and const
for variable declarations. No advanced techniques like closures, async/await, or Promises are employed.
Alternatives:
Other alternatives to consider for benchmarking similar scenarios:
buffer
module to store and manipulate binary data.Note that these alternatives may require modifications to the benchmark test code to accommodate the new APIs or libraries being used.