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;
}
--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 | 1286.8 Ops/sec |
typedArray sort | 34519.8 Ops/sec |
array i/o | 1300.9 Ops/sec |
typedArray i/o | 1208.9 Ops/sec |
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark definition, which includes the script preparation code, HTML preparation code (which is empty in this case), and an array of individual test cases.
Benchmark Definition
The benchmark definition specifies two types of arrays: regular Array
and Float32Array
. Both are used to store random integers between 0 and 1 million. The script preparation code initializes these arrays, populates them with random values, and then creates two functions: getRandomInt(max)
for generating random integers within a specified range, and two map functions to populate the arrays.
Options Compared
The benchmark compares two approaches:
a
):Array
object.map()
method.ta
) (specifically, Float32Array
):map()
method.Pros and Cons
Here are some pros and cons of each approach:
Regular Array (a
):
Pros:
Cons:
Typed Array (ta
) (specifically, Float32Array
):
Pros:
Cons:
Individual Test Cases
Each test case measures the execution time of a specific operation on both regular array (a
) and typed array (ta
):
a.sort()
.a
.ta
.These test cases provide insights into the relative performance differences between regular arrays and typed arrays under specific scenarios.
Library Used
None, as this benchmark definition uses only built-in JavaScript features and does not rely on any external libraries.