Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice VS splice VS shift: who is the fastest to keep constant size [VARIANT]
(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:
2 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(1);
splice
list.push('splice'); list.splice(1, list.length-1);
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:
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/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
404.5 Ops/sec
splice
5174035.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark is designed to measure the performance difference between three approaches for manipulating arrays in JavaScript: `slice`, `splice`, and `shift`. The benchmark consists of two test cases: 1. **`slice`**: Creates a copy of the array using the `slice()` method. 2. **`splice`**: Removes elements from the end of the array using the `splice()` method. The benchmark generates an array with 100,000 elements and then performs one million pushes to this array. The performance difference between these three approaches is then measured in terms of execution time per second. **Comparison Options** There are two main comparison options: * **`slice`**: Creates a new array by copying the original array using `slice()`. * Pros: Easy to understand, creates a new array without modifying the original. * Cons: Can be slower than other methods due to the overhead of creating a new array. * **`splice`**: Modifies the original array in place by removing elements from the end. * Pros: Can be faster than `slice` since it modifies the original array without creating a copy. * Cons: Modifies the original array, which can lead to unexpected behavior if not handled carefully. **Other Considerations** In addition to these two options, there is another approach that uses the `shift()` method: * **`shift`**: Removes elements from the beginning of the array without creating a new copy. * Pros: Can be faster than `slice` since it modifies the original array without creating a copy. * Cons: Requires care when handling edge cases, such as empty arrays. **Library and Special JS Features** There is no library used in this benchmark. However, the benchmark does utilize some special JavaScript features: * **Array methods**: The benchmark uses `slice()`, `splice()`, and `shift()` methods to manipulate arrays. * **Looping constructs**: The benchmark uses a `for` loop to push elements onto the array. **Benchmark Result** The latest benchmark result shows that the Chrome 119 browser with a Desktop platform and Mac OS X 10.15.7 operating system achieved: * **`splice`**: Execution time of approximately 9,019,286 executions per second. * **`slice`**: Execution time of approximately 383 executions per second. These results indicate that the `splice()` method is significantly faster than the `slice()` method in this benchmark.
Related benchmarks:
slice VS splice: who is the fastest to keep constant size
slice VS splice (clone) VS shift: who is the fastest to keep constant size
slice VS splice VS shift: who is the fastest to keep constant size (fork no string push)
slice VS splice VS shift: who is the fastest to keep constant size 100
Comments
Confirm delete:
Do you really want to delete benchmark?