Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
shift vs slice 1 element
(version: 0)
shift vs slice 1 element
Comparing performance of:
shift vs slice
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array.from({ length: 120000 }, (_, i) => i)
Tests:
shift
arr.shift();
slice
arr.slice(1)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
shift
slice
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser/OS:
Firefox 140 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
shift
53557240.0 Ops/sec
slice
23103616.0 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares the performance of two methods for removing the first element from an array in JavaScript: `arr.shift()` and `arr.slice(1)`. **Description:** * **`arr.shift();`**: This method directly removes the first element from the array and returns it. It modifies the original array. * **`arr.slice(1)`**: This method creates a new array containing all elements from the second element (index 1) to the end of the original array. It does not modify the original array. **Options Compared:** * **Direct Removal vs. Creating a New Array:** The benchmark tests whether directly removing the first element (`shift()`) or creating a new array without the first element (`slice(1)`) is faster. **Pros and Cons:** * **`shift()`**: * **Pro:** Faster for removing the *first* element as it directly modifies the array, avoiding unnecessary array copying. * **Con:** Modifies the original array in place. If you need to keep the original array intact, `slice(1)` is a better option. * **`slice(1)`**: * **Pro:** Returns a *new* array, leaving the original array unchanged. Useful when you need multiple copies of the array with different modifications. * **Con:** Creates a new array copy, which can be slightly slower than `shift()` for removing only the first element because it involves copying data. **Other Considerations:** * **Array Size:** For very large arrays, the performance difference between `shift()` and `slice(1)` might become less significant. * **Frequency of Operation:** If you frequently need to remove the first element from an array, `shift()` is likely more efficient due to its direct modification. **Alternatives:** * **`splice()`**: A more versatile method that can remove elements at any index and add new elements. However, it might be slower than `shift()` for removing only the first element. Let me know if you have any other questions.
Related benchmarks:
`Array.slice(-1)[0]` vs `Array[Array.length]` for 10000 length
Shift vs Slice
Slice vs Shift (for 1 element)
Array slice vs shift (2)
Comments
Confirm delete:
Do you really want to delete benchmark?