function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
var a = [Array(10000)].map(_ => Math.random(1000000));
var ta = (new Int16Array(10000)).map(_ => Math.random(1000000));
a.reverse();
ta.reverse();
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 reverse | |
typedArray reverse | |
array i/o | |
typedArray i/o |
Test name | Executions per second |
---|---|
array reverse | 178945.8 Ops/sec |
typedArray reverse | 426782.0 Ops/sec |
array i/o | 59370.2 Ops/sec |
typedArray i/o | 51983.3 Ops/sec |
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, providing insights into the performance differences between various approaches.
Benchmark Definition JSON
The provided Benchmark Definition JSON defines a benchmark comparing Array
and Int16Array
. The script preparation code generates two arrays of size 10,000 with random integers. The HTML preparation code is empty.
Here's what's being tested:
Array
(a
) versus a typed array (Int16Array
, ta
). Typed arrays are designed to store specific data types, such as integers or floats, and provide better performance and memory efficiency.Comparison options
Two approaches are compared:
Array
using the reverse()
method.Int16Array
) using the same reverse()
method.Pros and cons of each approach
Library
The getRandomInt
function is a simple utility function that generates random integers within a specified range. It's not a library per se, but rather a helper function used to populate the arrays.
Special JavaScript feature or syntax
None are mentioned in the provided code snippet. However, it's worth noting that MeasureThat.net may use other features like Promise
or async/await
for benchmarking purposes, which would require additional context.
Other alternatives
If you were to rewrite this benchmark with different approaches, some options might include:
BigInt
instead of integers: This could provide better performance for very large numbers.Set
or Map
, against Array
.Keep in mind that the best approach will depend on your specific use case and requirements.