Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice VS splice VS shift: who is the fastest to split array
(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:
4 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'); let list2 = []; while (list.length) { list2.push(list.slice(1, 50)); list.splice(0, 50); }
splice
list.push('splice'); let list2 = []; while (list.length) { list2.push(list.splice(0, 50)); }
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:
7 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0
Browser/OS:
Firefox 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
7310661.5 Ops/sec
splice
8054742.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its various components. **Benchmark Definition** The benchmark is designed to compare the performance of three different approaches for splitting an array: using `slice`, `splice`, and `shift`. The description explains that `splice` and `shift` are faster because they mutate the original list, while `slice` creates a copy of the list. **Options Compared** The two options being compared are: 1. **`slice`**: This method creates a shallow copy of a portion of an array and returns it. 2. **`splice`**: This method removes elements from an array and returns them as an array. It modifies the original array. **Pros and Cons** * `Slice`: + Pros: Easy to use, doesn't modify the original array. + Cons: Creates a copy of the array, which can be slower for large arrays. * `Splice`: + Pros: Modifies the original array, can be faster than `slice`. + Cons: Requires more parameters (start and end indices), can be less intuitive to use. **Other Considerations** Both `splice` and `shift` modify the original array, which can lead to unexpected behavior if the original array is used elsewhere in the code. In contrast, `slice` creates a copy of the array, which can be beneficial for preserving the original data. **Library Used (if applicable)** None of the test cases use any external libraries that affect the benchmarking outcome. However, it's worth noting that some JavaScript engines or browsers might have additional optimizations or quirks when dealing with array manipulation methods. **Special JS Features/Syntax** The `slice` and `splice` methods are standard JavaScript methods for array manipulation. There are no special JavaScript features or syntax being used in this benchmark. **Other Alternatives** If you were to rewrite this benchmark, you could also consider the following alternatives: * Using `array.prototype.map()` instead of `slice` and modifying the original array using a callback function. * Using `array.prototype.reduce()` to split the array into chunks, which can be more efficient than using `splice`. * Using a third-party library like Lodash or Ramda for array manipulation, which might provide additional optimization opportunities. Keep in mind that these alternatives would require significant changes to the benchmarking code and might not produce comparable results.
Related benchmarks:
slice VS splice: 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
Slice vs Splice vs Shift for 1 item
Comments
Confirm delete:
Do you really want to delete benchmark?