Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array vs TypedArray write performance ttttttt
(version: 1)
Comparing performance of:
Array write vs TypedArray write
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100000).fill(0).map(_x => 0); var typedArray = new Uint32Array(100000).fill(0);
Tests:
Array write
const len = array.length; for (let i = 0; i < len; ++i) { array[i] = array[i] + 1; }
TypedArray write
const len2 = typedArray.length; for (let i = 0; i < len2; ++i) { typedArray[i] = typedArray[i] + 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array write
TypedArray write
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0
Browser/OS:
Firefox 133 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array write
11182.0 Ops/sec
TypedArray write
22334.2 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark tests the performance of two approaches to writing data to a collection in JavaScript: using a regular JavaScript array and using a typed array (specifically, a `Uint32Array`). ### Options Compared 1. **JavaScript Array (`array`)**: - The benchmarked code iterates over a standard JavaScript array and increments each element by 1. - Testing Code Snippet: ```javascript const len = array.length; for (let i = 0; i < len; ++i) { array[i] = array[i] + 1; } ``` 2. **Typed Array (`typedArray`)**: - The benchmarked code performs the same operation on a typed array (`Uint32Array`), which is designed for handling binary data in a more efficient way. - Testing Code Snippet: ```javascript const len2 = typedArray.length; for (let i = 0; i < len2; ++i) { typedArray[i] = typedArray[i] + 1; } ``` ### Performance Results - The results show that the `TypedArray` write operation executes approximately **22,334** times per second, while the standard array write operation executes about **11,182** times per second. This indicates that writing to a typed array is significantly faster in this scenario. ### Pros and Cons 1. **JavaScript Array**: - **Pros**: - Flexibility: Can hold any data type (numbers, objects, strings, etc.). - Dynamic sizing: Arrays can grow and shrink as needed. - **Cons**: - Slower performance, especially for numerical operations due to dynamic typing and the need for JavaScript to manage the different types of elements. - Additional overhead due to managing type conversions and maintaining the length of the array. 2. **Typed Array** (`Uint32Array`): - **Pros**: - Improved performance for numerical operations, as elements of a typed array are stored in a contiguous block of memory and have a fixed size (32 bits per element). - More efficient memory usage when working with large datasets of uniform data types. - **Cons**: - Limited to specific types (in this case, 32-bit unsigned integers). - Fixed size: Typed arrays have a predetermined size upon creation and cannot be resized dynamically. ### Considerations - **Use Cases**: - Use typed arrays when dealing with large collections of numerical data, particularly in applications involving graphics (like WebGL), data processing, or any scenario where performance is critical. - Standard arrays might still be preferred for general-purpose storage where flexibility is more important than performance. ### Alternatives - **Other Typed Arrays**: Besides `Uint32Array`, JavaScript provides other typed arrays like `Int32Array`, `Float32Array`, and `Float64Array`, which can be used depending on the required precision and data type. - **Dynamic Arrays**: If operations that require mixed data types or dynamic resizing are frequent, using standard arrays may be more appropriate despite the performance hit. Overall, this benchmark clearly illustrates the performance advantages of typed arrays for numeric data manipulation, encouraging developers working with JavaScript to consider their data storage choices based on their specific performance and flexibility needs.
Related benchmarks:
array vs int32array2
Array vs Typed Array vs Buffer Typed Array
Direct Array vs Typed Array vs Array read performances
Array vs TypedArray write performance
TypedArray set() vs setter performance
Get and set in typed array vs simple array
Another array[len - 1] vs array.at(-1)
Push to: array vs float32array performance test 3
Array vs TypedArray write performance %%%%%%%%%%%%
Comments
Confirm delete:
Do you really want to delete benchmark?