Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Slice vs Splice vs Shift 231
(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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.slice()
11144529.0 Ops/sec
array.splice()
54110036.0 Ops/sec
array.shift()
103068936.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare the speed of three different methods for removing elements from the beginning of an array: 1. `array.slice(1)`: This method creates a new array that includes all elements except the first two. 2. `array.splice(0, itemsToRemove)`: This method removes `itemsToRemove` elements from the beginning of the array and returns them as an array. 3. `array.shift()`: This method removes and returns the first element from the array. **Options Compared** The benchmark is comparing the performance of these three methods on a fixed-size array with 9 elements, specifically removing the first 1 or 2 elements. **Pros and Cons of Each Approach** * `array.slice(1)`: + Pros: Efficient and concise. + Cons: Creates a new array, which can be memory-intensive for large arrays. * `array.splice(0, itemsToRemove)`. + Pros: Modifies the original array in-place. + Cons: Can be slower due to the extra operation of removing elements. * `array.shift()`. + Pros: Simple and efficient for small arrays or when only removing a single element. + Cons: May not be as efficient for larger arrays, as it involves multiple operations. **Library Used** There is no library explicitly mentioned in the benchmark definition. However, all three methods use native JavaScript array functions, which means they rely on built-in engine optimizations and do not require any additional libraries to execute. **Special JS Feature or Syntax** The benchmark does not mention any special JavaScript features or syntax that would require a specific execution environment or compiler. The code is written in vanilla JavaScript and should run on most modern browsers without issues. **Other Alternatives** For removing elements from the beginning of an array, other methods might include: * `array.pop()`: Removes and returns the last element from the array. * `array.concat()`: Creates a new array with the first element removed (although this is not exactly what you're looking for). However, these alternatives are not tested in the provided benchmark. **Benchmark Preparation Code** The script preparation code creates an array of 9 elements (`var array = [1, 2, 3, 4, 5, 6, 7, 8]`) and defines a variable to specify the number of items to remove (`var itemsToRemove = 1`). The `itemsToRemove` value is fixed at 1 for all three test cases. **Individual Test Cases** Each test case represents one of the three methods being compared: * `array.slice(1)`: Removes the first 2 elements (using `slice(1)`). * `array.splice(0, itemsToRemove)`: Removes the specified number of elements from the beginning of the array (using `splice(0, itemsToRemove)`). * `array.shift()`: Removes and returns the first element from the array (using `shift()`). The benchmark results show the execution frequency per second for each test case on a specific device running Mobile Safari 16.
Related benchmarks:
Slice vs Splice vs Shift
Slice vs Splice vs Shift (Large Array)
Slice vs Splice vs Shiftxxxxxx
Slice vs Splice vs Shift for 1 item
Comments
Confirm delete:
Do you really want to delete benchmark?