Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Push vs Splice
(version: 0)
Testing whether Push vs Slice to add an element to the list has a significant performance difference.
Comparing performance of:
Splice to add to list vs Push to add to list
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
Splice to add to list
let list = []; for (let i = 0; i < 1e6; ++i) list.splice(i, 0, i); list = [];
Push to add to list
let list = []; for (let i = 0; i < 1e6; ++i) list.push(i); list = [];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Splice to add to list
Push to add to list
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Splice to add to list
15.4 Ops/sec
Push to add to list
203.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark. **What is being tested?** The provided JSON represents two test cases that compare the performance of adding an element to an array using two different methods: `push()` and `splice()`. The test case uses a simple JavaScript loop to generate a large array with 1 million elements, which are then added to the array using either `push()` or `splice()`. After the loops are completed, the arrays are reset. **Options compared** The two options being compared are: * `push()`: This method adds an element to the end of an array and returns the new length of the array. It's a more convenient way to add elements to an array, as it doesn't require manually calculating the index where the new element should be inserted. * `splice()`: This method inserts or removes elements in an array at a specified position (0-based index). When used with the first argument being 0 and the second argument being the number of elements to add, it adds elements to the beginning of the array. **Pros and Cons** * `push()`: + Pros: more convenient, easier to use, and faster for small arrays. + Cons: can be slower for large arrays due to the overhead of calculating the index and potentially shifting elements around. * `splice()`: + Pros: allows for more flexibility and control over inserting elements at specific positions. + Cons: can be slower than `push()` due to the overhead of calculating the index, checking bounds, and potentially shifting elements around. **Other considerations** * The test case uses a simple JavaScript loop to generate the large array, which may not accurately represent real-world scenarios where arrays are populated with more complex data structures. * The use of `let` declarations for the array variable ensures that it's scoped to the test case, which helps prevent external variables from interfering with the test results. **Library usage** There is no library explicitly mentioned in the provided JSON. However, MeasureThat.net typically uses a custom JavaScript framework or library to execute benchmarks and collect performance data. **Special JS features/syntax** None are explicitly used in this benchmark. The code only relies on standard JavaScript syntax and built-in methods. **Alternatives** If you're interested in exploring alternative approaches to adding elements to an array, here are some examples: * Using `unshift()`: This method is similar to `push()` but adds elements to the beginning of the array instead. * Using `Array.prototype.concat()`: This method creates a new array with the elements from the original array and the additional elements. * Using a library like Lodash's `_.initial()` function: This function returns the first element of an array, which can be used as an alternative to manually calculating indices. Keep in mind that these alternatives may have different performance characteristics depending on the specific use case.
Related benchmarks:
slice VS splice: who is the fastest to keep constant size
slice VS splice VS shift: who is the fastest to keep constant size (fork no string push)
slice VS splice VS shift: who is the fastest to keep constant size [VARIANT]
Slice vs Splice vs Shift for 1 item
slice VS splice 1234567
Comments
Confirm delete:
Do you really want to delete benchmark?