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 < 4096; i += 4) {
if (i === 0) {
view.setFloat64(i, 1);
} else {
view.setFloat64(i, view.getFloat64(i - 4) + 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 < 4096; i += 4) {
if (i === 0) {
view.setFloat64(i, 1);
} else {
view.setFloat64(i, view.getFloat64(i - 4) + 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; | 310032.2 Ops/sec |
view.setFloat64(i, i) | 81392.9 Ops/sec |
array[i] = i; 2 | 308860.8 Ops/sec |
view.setFloat64(i, i) 2 | 81428.7 Ops/sec |
This benchmark compares two different methods of manipulating numerical data in JavaScript: using TypedArray
(specifically Float64Array
) and using DataView
. The focus of this benchmarking is to evaluate how these two approaches handle updating values in arrays of floating-point numbers, particularly in the context of performance.
TypedArray (Float64Array):
Float64Array
, which is a type of TypedArray designed to handle 64-bit floating-point numbers. This approach involves creating an instance of Float64Array
and manipulating its elements directly.array[i] = i;
: This insert mechanism updates the array directly.array[i] = i; 2
: An identical test to the first, showing how consistency is measured across runs.DataView:
DataView
to abstractly manage a buffer of binary data. This allows access to individual bytes and enables efficient reading and writing of various numeric types, including Float64.view.setFloat64(i, i)
: Using setFloat64
to update values in the DataView.view.setFloat64(i, i) 2
: Similar to the first, providing another measure of consistency.Pros:
Cons:
Pros:
Cons:
math.js
or numeric.js
provide additional functionality for mathematical operations but may not outperform native implementations in terms of raw performance.In conclusion, the benchmark demonstrates the performance differences between manipulating numerical data using TypedArray
and DataView
, helping engineers understand which approach might be more suitable for their specific use cases based on performance needs and data handling requirements.