function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
var a = [Array(10000)].map(_ => Math.random(1000000));
var ta = (new Float32Array(10000)).map(_ => Math.random(1000000));
a.sort();
ta.sort();
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 sort | |
typedArray sort | |
array i/o | |
typedArray i/o |
Test name | Executions per second |
---|---|
array sort | 737.2 Ops/sec |
typedArray sort | 17173.0 Ops/sec |
array i/o | 26020.6 Ops/sec |
typedArray i/o | 2650.4 Ops/sec |
Let's break down the provided JSON and explain what's being tested in each benchmark.
Benchmark Definition
The first part of the JSON represents a benchmark definition, which outlines the specific test case:
{
"Name": "array vs float32array without conversion",
"Description": null,
"Script Preparation Code": "function getRandomInt(max) {\r\n return Math.floor(Math.random() * Math.floor(max));\r\n}\r\nvar a = [...Array(10000)].map(_ => Math.random(1000000));\r\nvar ta = (new Float32Array(10000)).map(_ => Math.random(1000000));",
"Html Preparation Code": null
}
This benchmark tests the performance of two arrays: a
and ta
. The script preparation code initializes these arrays with 10,000 elements each, generated randomly. Note that there's no explicit conversion between a
and ta
, which is a key aspect of this test.
Individual Test Cases
The second part of the JSON represents multiple individual test cases:
[
{
"Benchmark Definition": "a.sort();",
"Test Name": "array sort"
},
{
"Benchmark Definition": "ta.sort();",
"Test Name": "typedArray sort"
},
{
"Benchmark Definition": "for (let i = 0; i < 10000; ++i) {\r\n a[i] = a[i] + 1;\r\n}",
"Test Name": "array i/o"
},
{
"Benchmark Definition": "for (let i = 0; i < 10000; ++i) {\r\n ta[i] = ta[i] + 1;\r\n}\r\nlet tb = Array.from(ta);",
"Test Name": "typedArray i/o"
}
]
These test cases compare the performance of different operations on arrays:
array sort
: Sorting an array in ascending order.typedArray sort
: Sorting a typed array (Float32Array
in this case) in ascending order.array i/o
: Incrementing each element in the array by 1 and then reading back the modified elements.typedArray i/o
: Performing the same operation as above, but using a typed array.Library:
The getRandomInt(max)
function is not a built-in library; it's a custom function created to generate random integers. However, Float32Array
is a part of the JavaScript standard library, which represents an array of 32-bit floating-point numbers. It's used in this benchmark to test typed arrays.
Special JS Feature or Syntax:
None are explicitly mentioned, but note that some JavaScript versions may have specific optimizations or behaviors for certain array operations. However, these are not explicitly highlighted as special features or syntax in the provided JSON.
Other Considerations:
The benchmarks focus on the performance differences between arrays and typed arrays without explicit conversions. This highlights the importance of optimized data structures and efficient memory management in JavaScript applications.
Alternatives:
If you were to rewrite this benchmark, consider alternative test cases or variations:
reduce()
, every()
).Int32Array
, Uint16Array
).These alternatives could provide a more comprehensive understanding of JavaScript's performance characteristics for different data structures and operations.