Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array push test
(version: 0)
Comparing performance of:
push vs preallocated vs intarray vs float array
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var NUMBER_OF_ELEMENTS = 10**5 var ELEMENT_LEN_BYTES = 4
Tests:
push
var array = []; var startNum1 = Math.random() for (let i = 1; i <= NUMBER_OF_ELEMENTS; i++) { array.push(i + startNum1) }
preallocated
var preAllocatedArray = new Array(NUMBER_OF_ELEMENTS); var startNum2 = Math.random() for (let i = 1; i <= NUMBER_OF_ELEMENTS; i++) { preAllocatedArray[i - 1] = i + startNum2; }
intarray
var intArray = new Uint32Array(NUMBER_OF_ELEMENTS) var startNum3 = Math.random() for (let i = 0; i < NUMBER_OF_ELEMENTS; i++) { intArray[i] = i + 1 + startNum3 }
float array
var f32Array = new Float32Array(NUMBER_OF_ELEMENTS) var startNum4 = Math.random() for (let i = 0; i < NUMBER_OF_ELEMENTS; i++) { f32Array[i] = i + 1 + startNum4 }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
push
preallocated
intarray
float array
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks to measure the performance of different approaches for array push operations. **Benchmark Definition JSON** The provided JSON defines a benchmark with four test cases: 1. **Array push test**: This is the main benchmark, which tests how fast an array can be pushed with elements. 2. **Preallocated**: This test case preallocates an array with `NUMBER_OF_ELEMENTS` elements before starting the loop. 3. **Int32Array**: This test case uses a typed array (`Uint32Array`) to store integers instead of regular arrays. 4. **Float32Array**: This test case uses another typed array (`Float32Array`) to store floats. **Test Cases** Each test case has its own script preparation code, which defines the variables and data structures used in the benchmark: * `push` test: Creates an empty array and pushes elements into it using the `push()` method. * `preallocated` test: Creates a preallocating array with `NUMBER_OF_ELEMENTS` elements and then pushes elements into it using indexed assignment (`array[i - 1] = i + startNum2`). * `intarray` test: Creates an integer typed array with `NUMBER_OF_ELEMENTS` elements and then pushes elements into it using the `push()` method, but using a different indexing strategy. * `float array` test: Similar to the `push` test, but uses a float typed array. **Library Usage** In the provided benchmark, no external libraries are used. The tests only rely on built-in JavaScript features and arrays. **Special Features/Syntax** The `preallocated` test case uses indexed assignment (`array[i - 1] = i + startNum2`) to push elements into the preallocating array. This is a more efficient approach than using the `push()` method, as it avoids creating a new element object each time. **Pros and Cons** Here are some pros and cons of each test case: * **Push**: Pros: simple to implement, widely supported. Cons: may have overhead due to creating a new element object each time. * **Preallocated**: Pros: more efficient by avoiding element creation, but requires preallocating the array. Cons: requires extra memory for the preallocating array. * **Int32Array**: Pros: uses typed arrays for better performance and memory management. Cons: may not be supported in older browsers or environments. * **Float32Array**: Similar to `intarray`, uses typed arrays for better performance and memory management. **Alternatives** Other alternatives for measuring array push operations could include: * Using a library like `fast-memoize` to memoize the results of expensive function calls, including array pushes. * Implementing custom array push algorithms using assembly language or other low-level languages to minimize overhead. * Using GPU-accelerated libraries like WebGL or CUDA to offload array operations to the graphics processing unit. However, these alternatives would likely require significant changes to the benchmark and may not be necessary for most use cases.
Related benchmarks:
Add to array
array test
array test
array test
Array push an element vs spread (fixed)
Comments
Confirm delete:
Do you really want to delete benchmark?