var size = 4 * 1000;
var buf = new ArrayBuffer(size);
var view = new DataView(buf);
var u32view = new Uint32Array(buf);
var data = [];
for (let i = 0; i < size/4; i++) {
data[i] = 10000*Math.random();
}
for (let i = 0, len = size/4; i < len; i++) {
view.setUint32(i*4, data[i]);
}
for (let i = 0, len = size/4; i < len; i++) {
u32view[i] = data[i];
}
u32view.set(data, 0)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
DataView | |
Uint32Array loop | |
Uint32Array set |
Test name | Executions per second |
---|---|
DataView | 14014.7 Ops/sec |
Uint32Array loop | 15681.5 Ops/sec |
Uint32Array set | 968071.1 Ops/sec |
Let's break down what's being tested in this benchmark.
What is being tested?
The benchmark is comparing the performance of three different approaches to write data into a Uint32Array
:
DataView
class to create a view into an underlying ArrayBuffer, and then using the setUint32
method to write 32-bit unsigned integers into the array.Uint32Array
from the same ArrayBuffer as above, and then iterating over the array using a for loop to write each element individually.set
method of the Uint32Array
class to write all elements into the array at once.Options compared
The benchmark is comparing the performance of these three approaches:
setUint32
method to write data into the array.set
method to write all elements into the array at once.Pros and cons of each approach
DataView
object, which can incur overhead in some cases.setUint32
when writing large amounts of data, as it involves iteration over the array.Library/Tool
The benchmark uses JavaScript's built-in DataView
class, which provides a view into an underlying ArrayBuffer. The Uint32Array
class is also used in conjunction with the DataView
.
Special JS feature/syntax
There are no special JavaScript features or syntax used in this benchmark that would require additional explanation.
Alternatives
If you're interested in exploring alternative approaches to writing data into a Uint32Array
, here are some options:
Buffer
class in Node.js.