Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice VS splice VS shift: best performance
(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 vs shift
Created:
5 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);
shift
list.push('splice'); list.shift();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
slice
splice
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):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition JSON** The benchmark definition represents a JavaScript microbenchmark that measures the performance of three different approaches to modify an array: 1. `splice` 2. `shift` 3. `slice` The description states that `splice` and `shift` are faster because they mutate the original list, while `slice` is slower because it creates a copy of the list. **Script Preparation Code** The script preparation code generates a large array of 1 million elements using `push`. **Html Preparation Code** There is no HTML preparation code provided, which means that this benchmark only measures the JavaScript execution time. **Individual Test Cases** Each test case represents a different approach to modify the array: 1. **`slice`**: The script pushes an element onto the list and then uses `slice` to create a new slice of the list starting from index 1. 2. **`splice`**: The script pushes an element onto the list and then uses `splice` to remove the first element from the list. 3. **`shift`**: The script pushes an element onto the list and then uses `shift` to remove the first element from the list. **Library** None of the test cases use a library, which means that the results are only dependent on the JavaScript engine being tested. **Special JS Features or Syntax** There is no special JavaScript feature or syntax used in any of the test cases. They all use standard JavaScript methods and operators. Now, let's discuss the pros and cons of each approach: 1. **`splice`**: Pros: Mutates the original list, which can be beneficial for performance-critical applications. Cons: Can lead to unexpected side effects if not used carefully. 2. **`shift`**: Pros: Also mutates the original list, which can improve performance. Cons: Requires an additional operation to access the removed element, which might incur a small overhead. 3. **`slice`**: Pros: Creates a new slice of the list, which is often safer and more predictable than modifying the original list. Cons: Can be slower due to the creation of a new object. In this benchmark, `splice` and `shift` are faster because they mutate the original list, while `slice` is slower because it creates a copy. However, in general, `slice` is considered a safer and more predictable approach, especially when working with large datasets or in applications where predictability is crucial. **Other Alternatives** If you were to modify this benchmark to include other approaches, some alternatives could be: * Using `concat()` instead of `slice` * Using `filter()` instead of `splice` or `shift` * Using array methods like `map()`, `forEach()`, or `reduce()` Keep in mind that each approach has its own trade-offs and may have different performance characteristics depending on the specific use case.
Related benchmarks:
slice VS splice (clone) VS shift: who is the fastest to keep constant size
slice VS splice VS shift: who is the fastest to split array
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: who is the fastest to keep constant size [VARIANT]
Comments
Confirm delete:
Do you really want to delete benchmark?