Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread vs splice vs concat vs push
(version: 0)
Comparing performance of:
spread vs concat
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const original = [] for (let i=0; i<=2000; i++) { original.push({i}) }
Tests:
spread
const original = [] for (let i=0; i<=2000; i++) { original.push({i}) } const result = {list: []} do { result.list = [...result.list, ...original.splice(0, 100)] } while (original.length > 0)
concat
const original = [] for (let i=0; i<=2000; i++) { original.push({i}) } const result = {list: []} do { result.list = result.list.concat(original.splice(0, 100)) } while (original.length > 0)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
spread
concat
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.1:latest
, generated one year ago):
Let's dive into the provided JSON that represents the benchmark. **Benchmark Definition** The benchmark is called "spread vs splice vs concat vs push". The description is empty, but we can infer from the test cases that it's about comparing the performance of different methods to create a new array by adding elements from an existing array. **Test Cases** There are two individual test cases: 1. **"spread"** * The benchmark definition creates an array `original` with 2000 elements and an object `result` with an empty array `list`. * It then uses the spread operator (`...`) to add 100 elements from `original` to `result.list` in a loop, until `original` is empty. 2. **"concat"** * The benchmark definition is identical to the "spread" case, but it uses the `concat()` method instead of the spread operator. **Latest Benchmark Result** The latest result shows two test runs: 1. **"concat"** * Browser: Chrome 98 * Device Platform: Desktop * Operating System: Mac OS X 10.15.7 * Executions Per Second (EPS): 56,771.11 2. **"spread"** * Browser: Chrome 98 * Device Platform: Desktop * Operating System: Mac OS X 10.15.7 * EPS: 30,481.44 **What's Being Tested** The test cases are comparing the performance of two methods to create a new array by adding elements from an existing array: 1. Using the spread operator (`...`) in combination with the `splice()` method. 2. Using the `concat()` method. **Pros and Cons of Each Approach** * **Spread Operator (`...`)**: + Pros: More concise syntax, easier to read and write. + Cons: May be slower than other methods due to the overhead of creating a new array with each iteration. * **Concat Method (`concat()`)** + Pros: Can be faster than using the spread operator, especially for large arrays. + Cons: Less concise syntax, may require more memory allocation. **Other Considerations** * The test case uses the `splice()` method to remove elements from the original array, which can be slower than other methods like `shift()` or `pop()`. * The loop condition is `original.length > 0`, which may not be the most efficient way to iterate over the array. * The benchmark result shows a significant difference in performance between the two test cases. This suggests that the spread operator may have overhead, and using the `concat()` method can lead to better performance. **Other Alternatives** In addition to the spread operator and concat method, there are other ways to create a new array by adding elements from an existing array: * Using a loop with `push()` or `unshift()`: This approach is simple but may be slower than using the spread operator or concat method. * Using `map()`: This method can be used to transform each element of the original array and return a new array. However, it may not be the most efficient way to simply add elements from an existing array. * Using libraries like Lodash or Underscore: These libraries provide functions like `union()` or `merge()` that can be used to create a new array by adding elements from an existing array. However, using external libraries may introduce additional overhead and dependencies. I hope this explanation helps you understand the benchmark test cases!
Related benchmarks:
Array construct vs array push vs array concat
Splice vs Spread vs Unshift to insert at beginning of array (fixed from slice)
spread large array (50000 elements) performance (vs slice, splice, concat, for)
Splice vs Spread vs Unshift vs Concat to insert at beginning of array (fixed from slice)
array.splice vs for loop for arrays
Comments
Confirm delete:
Do you really want to delete benchmark?