Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
splice vs slice split array
(version: 0)
Comparing performance of:
slice vs splice
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [...Array(1000)].map((_, index) => `item-${index}`); var SPLIT_LENGTH = 555;
Tests:
slice
const a = arr.slice(0, SPLIT_LENGTH); const b = arr.slice(SPLIT_LENGTH); console.log(a.length, b.length);
splice
const a = arr; const b = a.splice(SPLIT_LENGTH, a.length - SPLIT_LENGTH); console.log(a.length, b.length);
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:
5 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:145.0) Gecko/20100101 Firefox/145.0
Browser/OS:
Firefox 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
162645.2 Ops/sec
splice
231838.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and considered. **Benchmark Overview** The benchmark compares the performance of two approaches to split an array in JavaScript: `Array.prototype.slice()` and `Array.prototype.splice()`. The test case creates a large array with 1000 elements, then splits it into two parts at a specific index (`SPLIT_LENGTH = 555`). **Comparison Options** There are two comparison options: 1. **Slice**: Uses the `slice()` method to create a shallow copy of the original array from an index to a specified end index. 2. **Splice**: Uses the `splice()` method to remove elements from the original array starting from a specific index and removing a specified number of elements. **Pros and Cons** * **Slice**: + Pros: Generally faster, as it creates a shallow copy of the array without modifying the original. + Cons: Returns a new array object, which may incur additional memory allocation overhead. * **Splice**: + Pros: Modifies the original array in place, reducing memory allocation overhead. + Cons: Slower due to the modification of the original array and potential reallocation of elements. **Library Usage** None of the test cases uses a library, as both `slice()` and `splice()` are built-in JavaScript methods. **Special JS Features/Syntax** Neither test case explicitly uses any special JavaScript features or syntax. The focus is on comparing the performance of the two array splitting methods. **Other Considerations** * **Array size**: The large array size (1000 elements) may introduce additional overhead due to memory allocation and garbage collection. * **Browser differences**: Different browsers may have varying performance characteristics for these operations, which is reflected in the benchmark results. **Alternatives** If you wanted to explore other alternatives, some possible options could be: * Using `Array.prototype.map()` instead of `slice()`, as it can create a new array with the same elements. * Using `Array.prototype.reduce()` or other aggregation methods instead of splitting the array. * Comparing performance of other array manipulation methods, such as `forEach()`, `filter()`, or `every()`. Keep in mind that these alternatives may introduce additional complexity and overhead, which could affect the results of the benchmark.
Related benchmarks:
Slice & Splice vs ES6 Array Spread
Slice vs Splice delete
Slice vs Splice delete 1000
Slice vs splice forked
Slice vs splice first three elements
Comments
Confirm delete:
Do you really want to delete benchmark?