Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Splice vs Unshift (at least addition last elements 2)
(version: 1)
Comparing performance of:
array.splice() vs array.shift()
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [1, 2, 3, 4, 5, 6, 7, 8]
Tests:
array.splice()
array.splice(0, array.length-2, 77)
array.shift()
array.unshift(77)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array.splice()
array.shift()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.4 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 18 on iOS 18.4
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.splice()
74335832.0 Ops/sec
array.shift()
6202673.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark provided compares two JavaScript array manipulation methods: `splice()` and `unshift()`, focusing on how they perform in terms of execution speed when adding elements to the start of an array. ### Comparison of Options 1. **array.splice()**: - **Usage**: This method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. In this benchmark, it is used to add the number `77` at the start of the array while removing the last two elements. - **Performance**: The benchmark shows high execution speed: approximately 14.55 million executions per second. - **Pros**: - Versatile: Can remove and insert elements simultaneously. - Allows fine control over positions of elements in the array. - **Cons**: - More complex in terms of functionality, as it has more overhead due to its versatility. - Potentially less efficient for simply adding an element at the start. 2. **array.unshift()**: - **Usage**: This method adds one or more elements to the beginning of an array and returns the new length of the array. Here, it adds the number `77` to the start of the array. - **Performance**: The benchmark indicates a significantly lower execution speed: approximately 565,336 executions per second. - **Pros**: - Simple and straightforward; specifically designed for adding elements to the start of an array. - Easier to read and understand for this specific use case. - **Cons**: - Unlike `splice()`, it does not allow for the removal of elements at the same time. - May not be as efficient as `splice()` for operations that require both inserting and removing elements. ### Library and Syntax Considerations Both these methods are native to JavaScript's built-in `Array` object and do not require any external libraries. The benchmark does not utilize any special JavaScript features or syntax beyond standard array manipulation methods. ### Alternative Approaches - **Using Array.concat()**: Instead of using `splice()` or `unshift()`, you could create a new array that combines the new elements with the existing array. This can be done using `array = [77].concat(array)`: - **Pros**: It avoids mutating the original array, which can be advantageous in certain programming paradigms (like functional programming). - **Cons**: This method creates a new array, which may incur additional memory overhead and could be less efficient than in-place methods. - **Array Destructuring**: With ES6, you could use array destructuring for a similar effect: `array = [77, ...array]`. - **Pros**: This syntax is clear and concise, promoting readability. - **Cons**: It also creates a new array, resulting in memory allocation overhead. In summary, the benchmark illustrates the performance differences between two common methods for modifying arrays in JavaScript, highlighting their practical applications, advantages, and drawbacks. Understanding these methods can help engineers make informed decisions in choosing the right approach based on their specific use cases.
Related benchmarks:
Slice vs splice
splice vs unshift
Slice vs splice forked
Slice vs splice 2 ...
splice vs shift 3
Splice vs shift to remove at beginning of array (fixed from slice)
Array splice vs delete
Splice vs Shift to remove from the beginning
Splice vs Shift perf
Empty array: Splice vs Shift
Comments
Confirm delete:
Do you really want to delete benchmark?