Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Slice vs Splice vs Shift for 1 item
(version: 0)
Compares the speed for removing 2 items from the beginning of an array
Comparing performance of:
array.slice() vs array.splice() vs array.shift()
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [1, 2, 3, 4, 5, 6, 7, 8] var itemsToRemove = 1
Tests:
array.slice()
array = array.slice(1)
array.splice()
array.splice(0, itemsToRemove)
array.shift()
array.shift()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
array.slice()
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.slice()
5727531.0 Ops/sec
array.splice()
30240254.0 Ops/sec
array.shift()
54843328.0 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares the performance of three methods for removing one item from the beginning of an array in JavaScript: * **`array.slice(1)`**: This method creates a new array containing all elements of the original array starting from the second element (index 1). It leaves the original array unchanged. * **`array.splice(0, 1)`**: This method modifies the original array by removing one element at index 0 (the first element). The `1` specifies the number of elements to remove. * **`array.shift()`**: This method also modifies the original array by directly removing the first element and returning that element. **Pros and Cons:** * **`slice()`**: * **Pros**: Doesn't modify the original array, preserving its state. * **Cons**: Creates a new array, which can be memory-intensive for large arrays. It might be slower than `splice()` or `shift()`. * **`splice()`**: * **Pros**: Modifies the original array in place, potentially saving memory. Can remove multiple elements at once by changing the second argument. * **Cons**: Slightly slower than `shift()` due to more complex operations. * **`shift()`**: * **Pros**: Fastest method as it directly modifies the array and doesn't create new arrays. * **Cons**: Only removes one element at a time. Modifies the original array. **Other Considerations:** The choice between these methods depends on your specific use case: * If you need to keep the original array intact, use `slice()`. * For modifying the array and efficiency, `shift()` is generally preferred for removing single elements. * `splice()` offers more flexibility for removing multiple elements or inserting new ones at a specific position within the array. **Alternatives:** For truly large arrays, consider using libraries like Lodash or Ramda which often provide optimized versions of these common array operations.
Related benchmarks:
Slice vs Splice vs Shift
Slice vs Splice vs Shift (Large Array)
Slice vs Splice vs Shiftxxxxxx
Slice vs Splice vs Shift 231
Comments
Confirm delete:
Do you really want to delete benchmark?