var buffer1 = new ArrayBuffer(1024 * Float64Array.BYTES_PER_ELEMENT);
var array = new Float64Array(buffer1);
var buffer2 = new ArrayBuffer(1024 * Float64Array.BYTES_PER_ELEMENT);
var view = new DataView(buffer2);
for (let i = 0; i < 1024; i += 1) {
if (i === 0) {
array[i] = 1;
} else {
array[i] = array[i-1] + i;;
}
}
for (let i = 0; i < 1024; i += 1) {
if (i === 0) {
view.setFloat64(i, 1);
} else {
view.setFloat64(i, view.getFloat64(i - 1) + i);
}
}
for (let i = 0; i < 1024; i += 1) {
if (i === 0) {
array[i] = 1;
} else {
array[i] = array[i-1] + i;;
}
}
for (let i = 0; i < 1024; i += 1) {
if (i === 0) {
view.setFloat64(i, 1);
} else {
view.setFloat64(i, view.getFloat64(i - 1) + i);
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array[i] = i; | |
view.setFloat64(i, i) | |
array[i] = i; 2 | |
view.setFloat64(i, i) 2 |
Test name | Executions per second |
---|---|
array[i] = i; | 193591.4 Ops/sec |
view.setFloat64(i, i) | 46397.5 Ops/sec |
array[i] = i; 2 | 222832.0 Ops/sec |
view.setFloat64(i, i) 2 | 51341.1 Ops/sec |
The provided benchmark evaluates the performance of two different approaches to managing and manipulating binary data in JavaScript: Typed Arrays (specifically Float64Array
) and the DataView object. Their performance is assessed based on executing a series of similar operations involving storing and retrieving 64-bit floating-point numbers. The tests analyze the speed of executing operations over 1024 iterations using both approaches.
Typed Arrays (Float64Array
):
Float64Array
specifically allows for working with 64-bit floating-point numbers, which is significant for tasks that require high precision, such as high-performance calculations in data processing and graphics.DataView:
ArrayBuffer
regardless of the endianness of the underlying platform.The benchmark contains four individual test cases, two for Float64Array
and two for DataView
, which compare their performance through similar operations:
Test Case: array[i] = i;
and its duplicate:
Float64Array
, with the first index set to 1 and subsequent indices calculated based on the previous index plus the current iteration (i
).Test Case: view.setFloat64(i, i)
and its duplicate:
DataView
to set values in the same manner as the Typed Array, but it retrieves the previous value via view.getFloat64(i - 1)
before performing the addition.The results from the benchmark detail the number of executions per second for each of the test cases:
Float64Array
(array[i] = i;
) outperformed the DataView
operations (view.setFloat64(i, i)
) significantly in this context:array[i] = i; 2
: 222,832 executions/secondarray[i] = i;
: 193,591 executions/secondview.setFloat64(i, i) 2
: 51,341 executions/secondview.setFloat64(i, i)
: 46,397 executions/secondTyped Arrays:
DataView:
getFloat64
, setFloat64
).While the benchmark focuses on Typed Arrays and DataView, there are other alternatives depending on the use case:
This benchmark ultimately reflects the trade-offs between performance and flexibility in JavaScript when managing binary data. Understanding when to use each approach can enable developers to write faster and more efficient code in the right scenarios.