Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Slice vs Splice vs Shift (100)
(version: 0)
Compares the speed for removing 100 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, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200] var itemsToRemove = 100
Tests:
array.slice()
var narray = array.slice(itemsToRemove)
array.splice()
var narray = array.splice(0, itemsToRemove)
array.shift()
var narray = []; for (let i = 0 ; i < itemsToRemove ; i++) narray.push(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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different array methods is an essential task for any JavaScript developer. **Benchmark Overview** The benchmark compares three ways to remove elements from the beginning of an array: `slice()`, `splice()`, and `shift()`. **Methods Compared** 1. `array.slice(itemsToRemove)`: This method returns a new array containing the last `itemsToRemove` elements of the original array, effectively removing the first `itemsToRemove` elements. 2. `array.splice(0, itemsToRemove)`: This method modifies the original array by removing the first `itemsToRemove` elements and returns an array containing the removed elements. 3. `for (let i = 0 ; i < itemsToRemove ; i++) narray.push(array.shift())`: This method uses a loop to remove each of the first `itemsToRemove` elements from the original array using `shift()` and adds them to a new array (`narray`) until all elements are removed. **Pros and Cons** 1. **slice()**: Pros: efficient, simple, and widely supported. Cons: returns a new array, can be slower for large arrays due to memory allocation. 2. **splice()**: Pros: modifies the original array, can be faster than `slice()` for large arrays. Cons: requires modifying the original array, can be less predictable behavior if not used carefully. 3. **shift() + loop**: Pros: flexible, can handle arrays of any size. Cons: slower and more complex than `slice()` or `splice()`, may cause stack overflow for very large arrays. **Library Used** None. **Special JS Features/Syntax** * None mentioned in the benchmark definition or test cases. **Other Alternatives** For array removal methods, you can also consider: 1. `array.filter((_, index) => index >= itemsToRemove)`: This method returns a new array containing all elements of the original array except those at the first `itemsToRemove` indices. 2. `array.map((_, index) => index >= itemsToRemove ? undefined : value)`: This method returns an empty array, as it will skip over the first `itemsToRemove` elements. However, these alternatives may have different performance characteristics and use cases compared to the methods tested in this benchmark. **Benchmark Result Analysis** The latest benchmark result shows that: 1. `array.splice()` is the fastest method with an average execution speed of 7105294.5 executions per second. 2. `array.slice()` is slightly slower, with an average execution speed of 13915588.0 executions per second. 3. The custom `shift() + loop` approach is the slowest, with an average execution speed of 781259.0 executions per second. Keep in mind that these results are specific to this benchmark and may vary depending on other factors such as array size, JavaScript engine version, and hardware configuration.
Related benchmarks:
Remove by splice vs copyWithin
Remove element from array splice vs copyWithin
Slice vs Splice vs Shift for 1 item
Slice vs Splice vs Shift 231
Comments
Confirm delete:
Do you really want to delete benchmark?