Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread vs concat vs passing by reference
(version: 0)
Comparing performance of:
Spread vs Concat vs Pass by reference
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
Spread
function q(){ return [{}]; } let t = []; for (let i = 0; i < 1000; i++){ t = [...t, ...q()]; }
Concat
function q(){ return [{}]; } let t = []; for (let i = 0; i < 1000; i++){ t.concat(q()); }
Pass by reference
function q(arr){ arr.push({}) } const t = []; for (let i = 0; i < 1000; i++){ q(t); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Spread
Concat
Pass by reference
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 benchmark and its test cases. **What is being tested?** The benchmark measures the performance of three different approaches for creating arrays: 1. **Pass by reference**: This approach modifies the original array (`t`) by pushing elements to it using the `push()` method. 2. **Concatenation**: This approach uses the `concat()` method to add new elements to the end of the array. 3. **Spread operator**: This approach creates a new array and spreads its elements into the target array (`t`) using the spread operator (`...`). **Options comparison** The benchmark compares these three approaches in terms of performance, specifically how many executions per second each approach can handle. **Pros and cons:** 1. **Pass by reference**: * Pros: Modifies the original array, potentially more efficient since it doesn't create a new array. * Cons: May not be suitable for all use cases where immutability is required. 2. **Concatenation**: * Pros: Creates a new array each time, which can be beneficial when working with large datasets. * Cons: Can lead to performance issues if creating multiple arrays is unnecessary. 3. **Spread operator**: * Pros: Creates a new array and avoids modifying the original array, making it more predictable and easier to reason about. * Cons: May require additional memory allocations due to the creation of new arrays. **Library or syntax** None of the test cases use any external libraries. They rely solely on JavaScript's built-in features. **Special JS feature or syntax** The spread operator (`...`) is used in one of the test cases. The `concat()` method is also used, but it's a standard JavaScript method. **Other alternatives** If you want to explore alternative approaches, here are a few examples: * Using `Array.prototype.slice()` to create a shallow copy of an array and then modifying that copy. * Using `Array.prototype.reduce()` to accumulate elements into a new array. * Using a library like Lodash's `_.uniq` method to concatenate arrays while avoiding performance issues with large datasets. Keep in mind that the choice of approach depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
Array.prototype.concat vs Spread operator
Array.prototype.concat vs Spread operator
Array concat vs spread operator vs push (1)
Array.prototype.concat vs sequential spread operator
Array concat vs spread operator vs push with large arrays
Comments
Confirm delete:
Do you really want to delete benchmark?