Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
push vs splice mytest
(version: 0)
Comparing performance of:
push vs splice
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const iterations = 1000000; function push() { const arr = []; for (let i = 0; i < iterations; i++) { arr.push(i); } } function splice() { const arr = []; for (let i = 0; i < iterations; i++) { arr.splice(i, 0, i); } }
Tests:
push
push();
splice
splice();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
push
splice
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
gemma2:9b
, generated one year ago):
This benchmark on MeasureThat.net compares the performance of two methods for adding elements to an array in JavaScript: `push()` and `splice()`. **What is tested?** The benchmark measures how many times each method can add a new element to an array within a given timeframe (1 second). The 'iterations' variable determines the number of additions performed. Each test executes the function repeatedly, measuring the time it takes to complete a set number of iterations. **Options compared:** * **`push()`**: This is the simplest method for adding elements to the end of an array. It directly adds the new element to the end and returns the new length of the array. * **`splice()`**: This method is more versatile as it can insert, remove, or replace elements within an array at a specific index. For this benchmark, `splice()` is used with `i` as the starting index and 0 as the delete count, effectively inserting a new element at each iteration. **Pros & Cons:** * **`push()`: ** * **Pro**: Extremely fast and lightweight, optimized for appending elements. * **Con**: Only suitable for adding elements to the end of an array; less flexible than `splice()`. * **`splice()`: ** * **Pro**: More versatile, can add, remove, or replace elements at any index. * **Con**: Can be slower than `push()` as it involves more complex manipulation within the array. **Other Considerations:** The choice between `push()` and `splice()` depends on the specific use case. For frequently appending elements to an array's end, `push()` is generally the most efficient choice. If you need more control over element placement or require modifications beyond simple append, `splice()` provides greater flexibility. **Alternatives:** * **`Array.prototype.concat()`: ** Concatenates arrays, creating a new array with all elements combined. This is less efficient than `push()` for sequential additions to a single array. * **Libraries:** Some libraries like Lodash offer optimized functions for array manipulation, potentially providing performance benefits over built-in methods in certain scenarios. Let me know if you have any more questions!
Related benchmarks:
Subarray - Splice vs Slice
Splice vs Spread (immutable only)
splice vs spread operator for adding elements into very large 2D arrays
array.splice vs for loop for arrays
Comments
Confirm delete:
Do you really want to delete benchmark?