function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
var a = [Array(10000)].map(_ => Math.random(1000000));
var ta = (new Float64Array(10000)).map(_ => Math.random(1000000));
a.slice(0);
ta.slice(0);
for (let i = 0; i < 10000; ++i) {
a[i] = a[i] + 1;
}
for (let i = 0; i < 10000; ++i) {
ta[i] = ta[i] + 1;
}
let tb = Array.from(ta);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array slice | |
typedArray slice | |
array i/o | |
typedArray i/o |
Test name | Executions per second |
---|---|
array slice | 34233.9 Ops/sec |
typedArray slice | 42052.5 Ops/sec |
array i/o | 119236.4 Ops/sec |
typedArray i/o | 10578.4 Ops/sec |
I'd be happy to explain the benchmark being tested on MeasureThat.net.
Benchmark Definition The benchmark is designed to compare the performance of JavaScript arrays and TypedArrays (Float64Arrays) in various operations, specifically slicing and modifying elements.
Test Cases
There are four test cases:
array slice
: Measures the time it takes to execute the a.slice(0)
operation.typedArray slice
: Measures the time it takes to execute the ta.slice(0)
operation.array i/o
: Measures the time it takes to modify elements in an array using a loop (for (let i = 0; i < 10000; ++i) { a[i] = a[i] + 1; }
).typedArray i/o
: Measures the time it takes to convert a Float64Array to an Array and then modify elements in the resulting array (for (let i = 0; i < 10000; ++i) { ta[i] = ta[i] + 1; } let tb = Array.from(ta);
).Comparison of Options
In each test case, two options are compared:
Pros and Cons
Here's a brief summary of the pros and cons of each option:
array slice
:typedArray slice
:array i/o
:typedArray i/o
:Other Considerations
When choosing between arrays and TypedArrays, consider the following factors:
Library and Special JS Features
In the benchmark code, the following libraries are used:
Float64Array
: A typed array that represents a sequence of 64-bit floating-point numbers. This library is built-in to JavaScript.Array.from()
: A method that converts an iterable object into an Array. This method is part of the modern JavaScript API.No special JS features or syntax are used in this benchmark.
Alternatives
If you're looking for alternatives to MeasureThat.net, consider the following options:
Keep in mind that these alternatives may require more setup and configuration than MeasureThat.net.