const length = 1000;
const arrayBuffer = new ArrayBuffer(length);
const array = new Uint8Array(arrayBuffer);
for (let i = 0; i < length; i++) {
array[i] = 22;
}
const dataView = new DataView(arrayBuffer);
const resultArray = [];
for (let i = 0; i < length; i++) {
const value = dataView.getUint8(i);
resultArray.push(Boolean(value));
}
const length = 1000;
const arrayBuffer = new ArrayBuffer(length);
const array = new Uint8Array(arrayBuffer);
for (let i = 0; i < length; i++) {
array[i] = 22;
}
const dataView = new DataView(arrayBuffer);
const transferArray = new Uint8Array(dataView);
const resultArray = [transferArray].map(value => Boolean(value));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
readUint8 with for loop | |
Slice with map |
Test name | Executions per second |
---|---|
readUint8 with for loop | 7269.0 Ops/sec |
Slice with map | 436898.4 Ops/sec |
Let's break down the provided benchmark and its options.
What is being tested?
The benchmark compares two approaches to convert binary data from a DataView
object to an array of booleans:
getUint8()
method of the DataView
object to read individual bytes from the buffer and then pushes the result of Boolean(value)
onto an array using a for loop.slice()
method of the Uint8Array
object to extract a subset of bytes from the buffer, followed by applying the map()
function to convert each byte to a boolean value.Options comparison
The two approaches differ in their usage of native array methods:
slice()
and map()
, which are more concise but may have performance overhead due to additional function calls.Pros and cons
readUint8 with for loop:
Pros:
Cons:
Slice with map:
Pros:
Cons:
Library usage
The benchmark uses the following libraries/libraries-specific features:
DataView
: a native JavaScript object for working with binary data.Uint8Array
and ArrayBuffer
: native JavaScript objects for working with arrays and buffers.No external libraries are required. However, if you were to use alternative approaches that might involve external libraries (e.g., using Python or C++), you would need to consider the additional dependencies and performance implications.
Special JS features
There is no special JS feature or syntax being used in this benchmark beyond what's native to JavaScript.