Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS arrays..
(version: 1)
Comparing performance of:
Concat vs Push
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var toAppend = Array(1).fill("jhd"); var original = [ "first", "second", "third" ];
Tests:
Concat
var res = original.concat(toAppend);
Push
var res = original.push(...toAppend);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Concat
Push
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
gemma2:9b
, generated one year ago):
This benchmark tests two different methods for appending elements to an existing JavaScript array: `concat` and `push`. Let's break down each method and their pros/cons: **1. `concat()`** * **Benchmark Definition:** `var res = original.concat(toAppend);` * This method creates a *new* array containing all the elements from the original array (`original`) followed by the elements from the `toAppend` array. * **Pros:** * Doesn't modify the original array, preserving its original state. This can be crucial if you need to keep multiple copies of your data. * Returns a new array, allowing you to directly use the result without re-assigning it. * **Cons:** * Can be less performant than `push` for appending only one or two elements because it creates a completely new array. **2. `push()`** * **Benchmark Definition:** `var res = original.push(...toAppend);` * This method modifies the original array (`original`) by adding all the elements from `toAppend` to its end. It then returns the *modified* length of the array. * **Pros:** * In-place modification can be more memory-efficient, especially when dealing with large arrays. * Generally faster than `concat()` when appending multiple elements. * **Cons:** * Modifies the original array directly, which might not be desirable if you need to keep multiple versions of your data unchanged. **Other Considerations:** * **Context Matters:** The best approach depends heavily on your use case. If you frequently append small sets of elements and need to preserve the original array, `concat()` is a good option. For larger sets or when in-place modification is acceptable, `push()` might be more efficient. * **Libraries:** This benchmark doesn't involve any external libraries. **Alternatives:** * **`Array.prototype.splice(endIndex, deleteCount, item1, ..., itemN)`:** More versatile than `concat` and `push`, allowing you to insert elements at specific positions within an array, remove existing elements, and modify the array's content in more complex ways. Let me know if you have any other questions or want to explore specific scenarios in more detail!
Related benchmarks:
Splice vs Spread vs Unshift to insert at beginning of array
Splice vs Spread vs Unshift to insert at beginning of array (fixed from slice)
Splice vs Spread vs Unshift vs Push to insert at beginning of array
Splice vs Spread vs Unshift vs Concat to insert at beginning of array (fixed from slice)
Using Splice vs Spread vs Unshift to insert at beginning of array
Comments
Confirm delete:
Do you really want to delete benchmark?