var array = new Array(100000).fill(0);
for (let i=0; i<array.length; i++) {
array[i] = array[i] + 1;
}
var typedArray = new Uint8Array(100000).fill(0);
for (let i=0; i<typedArray.length; i++) {
typedArray[i] = typedArray[i] + 1;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array write | |
TypedArray write |
Test name | Executions per second |
---|---|
Array write | 9435.2 Ops/sec |
TypedArray write | 16594.9 Ops/sec |
Measuring JavaScript performance is an essential task in web development, and MeasuringThat.net provides a great platform for it.
The provided JSON represents two individual test cases: "Array write" and "TypedArray write". These tests aim to measure the performance of writing data to arrays versus typed arrays.
What are arrays and typed arrays?
In JavaScript, Array
is a built-in object that represents an array-like data structure. However, it's not designed for efficient storage or manipulation of binary data.
On the other hand, TypedArray
(e.g., Uint8Array
, Int32Array
, etc.) is a specialized type of array that's designed to work efficiently with binary data. Typed arrays provide better performance and memory usage compared to regular arrays when working with large amounts of numerical or binary data.
Options being compared
The two test cases compare the performance of writing data to regular arrays versus typed arrays. Specifically:
fill()
. It then increments each element in the array by 1.Uint8Array
of length 100,000 and fills it with zeros using fill()
. It then increments each element in the typed array by 1.Pros and Cons
Array write:
Pros:
Cons:
TypedArray write:
Pros:
Cons:
Other considerations
When deciding between Array
and TypedArray
, consider the following factors:
TypedArray
might be a better choice.TypedArray
.TypedArray
can help reduce memory usage.Library
The test case uses the native
JavaScript libraries for arrays and typed arrays.
Special JS feature/syntax
There are no special JS features or syntax used in this benchmark. However, it's worth noting that some older browsers might require additional polyfills or workarounds when using typed arrays.
Alternatives
If you're interested in exploring alternative approaches, here are a few options:
Keep in mind that these alternatives often come with their own trade-offs and requirements, so it's essential to carefully evaluate the pros and cons before choosing an approach for your specific use case.