Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test slice vs splice
(version: 0)
100k list splice and shift win, they mutate list slice loose, it creates a copy of list 7.5x slower
Comparing performance of:
slice vs splice
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var list = []; for (var i = 0; i < 1000 * 1000; i++) { list.push(i); }
Tests:
slice
list.push('slice'); list = list.slice(50000);
splice
list.push('splice'); list.splice(0, 50000);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
slice
splice
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
gemma2:9b
, generated one year ago):
This benchmark tests the performance of two methods for extracting a portion of an array in JavaScript: `slice()` and `splice()`. **Options Compared:** * **`slice()`:** This method creates a *new* array containing the elements from the specified start index to the end index (excluding the end index). It leaves the original array unchanged. * **`splice()`:** This method modifies the *original* array by removing elements from the specified start index and replacing them with new elements. **Pros/Cons:** * **`slice()`:** * **Pro:** Faster because it doesn't modify the original array, reducing overhead. It's ideal when you need a copy of a portion of an array without altering the original. * **Con:** Creates a new array object, which can consume more memory if dealing with large arrays. * **`splice()`:** * **Pro:** Modifies the original array directly, potentially saving memory if you don't need to keep the original array intact. Can also be used for inserting new elements into an array. * **Con:** Slower because it modifies the array in place, which can involve more complex operations. **Benchmark Results & Considerations:** The benchmark results show that `slice()` is approximately 7.5x faster than `splice()` in this particular scenario. This difference is likely due to the nature of the operation: `slice()` simply creates a new array copy, while `splice()` performs modifications on the original array. **Alternatives:** While not explicitly shown in the provided benchmark, other methods for working with portions of arrays include: * **`concat()`:** Creates a new array by joining together two or more existing arrays. * **Array.from():** Creates a new array from an iterable object (e.g., a string, an array-like object). Let me know if you have any other questions!
Related benchmarks:
slice VS splice VS shift: who is the fastest to keep constant size (fork no string push)
slice VS splice VS shift: removing an elmeent from the middle
slice VS splice VS shift: who is the fastest to keep constant size 100
slice VS splice VS shift: smaller list, copy last half of array
slice VS splice VS shift: who is the fastest to keep constant size [VARIANT]
Comments
Confirm delete:
Do you really want to delete benchmark?