Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Slice vs Splice vs Shift
(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:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [1, 2, 3, 4, 5, 6, 7, 8] var itemsToRemove = 2
Tests:
array.slice()
array = array.slice(2)
array.splice()
array.splice(0, itemsToRemove)
array.shift()
for (let i = 0 ; i < itemsToRemove ; i++) 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:
6 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.slice()
7841607.5 Ops/sec
array.splice()
18275440.0 Ops/sec
array.shift()
13345268.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's break down what's being tested in this benchmark. **What is being compared?** The benchmark is comparing three different ways to remove the first two elements from an array: 1. Using `slice()` 2. Using `splice()` 3. Using `shift()` (in a loop) **What are the pros and cons of each approach?** ### 1. Array.slice() * **Pros:** + Easy to use, concise code + Works well when you need to remove elements from anywhere in the array, not just the beginning * **Cons:** + Can be slower than `splice()` or `shift()`, especially for large arrays (as seen in the benchmark results) ### 2. Array.splice() * **Pros:** + Fast and efficient, especially for large arrays + Allows you to remove elements from anywhere in the array, not just the beginning * **Cons:** + Can be slower than `shift()` when removing elements from the end of a small array (although this is unlikely) + Requires specifying the index and count of elements to remove ### 3. Array.shift() * **Pros:** + Fast and efficient, especially for small arrays + Easy to use in a loop when you need to remove multiple elements from the beginning of an array * **Cons:** + Only works when removing elements from the beginning of the array + Can be slower than `splice()` or `slice()` when working with large arrays **Library used** None. The benchmark is using only built-in JavaScript methods (`slice()`, `splice()`, and `shift()`). **Special JS feature or syntax** None. **Other alternatives** * You can use the spread operator (`...`) in combination with `slice()` to remove elements, like this: `[...array.slice(2)]`. * You can also use a loop with `delete` to remove elements from the array, but this approach is generally slower and less efficient than using `splice()` or `shift()`. * If you need to remove elements from anywhere in the array, you can use a combination of `indexOf()` and `slice()` to find the index of the element to remove. Overall, the benchmark results suggest that `splice()` is the fastest method for removing elements from an array, followed closely by `shift()`. However, when working with small arrays or specific use cases, `slice()` may still be a good choice due to its ease of use and concise code.
Related benchmarks:
Slice vs Splice vs Shift (Large Array)
Slice vs Splice vs Shiftxxxxxx
Slice vs Splice vs Shift for 1 item
Slice vs Splice vs Shift 231
Comments
Confirm delete:
Do you really want to delete benchmark?