Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array concat vs Array.push.apply
(version: 0)
Comparing performance of:
Regular concat vs Array.push.apply
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Regular concat
let arrayA = new Array(500).fill("x",0,500); let arrayB = new Array(500).fill("y",0,500); arrayA = arrayA.concat(arrayB);
Array.push.apply
let arrayA = new Array(500).fill("x",0,500); let arrayB = new Array(500).fill("y",0,500); arrayA.push.apply(arrayA, arrayB);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regular concat
Array.push.apply
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regular concat
172923.7 Ops/sec
Array.push.apply
136226.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **What is being tested?** The provided benchmark tests two approaches to concatenate arrays: `arrayA.concat(arrayB)` and `arrayA.push.apply(arrayB)`. The test case creates two large arrays (`arrayA` and `arrayB`) filled with 'x' and 'y' characters, respectively, using the `new Array()` constructor and the `fill()` method. The benchmark then measures how long it takes to concatenate these arrays using each of the two approaches. **Options compared** The two options being tested are: 1. **`arrayA.concat(arrayB)`**: This is a more traditional way of concatenating arrays in JavaScript. It creates a new array and copies elements from both `arrayA` and `arrayB` into it. 2. **`arrayA.push.apply(arrayB)`**: This approach uses the `push()` method with an array as its argument, effectively applying each element of `arrayB` to `arrayA`. **Pros and Cons** * **`arrayA.concat(arrayB)`**: * Pros: More straightforward and intuitive. Can be easier to read and maintain. * Cons: Creates a new array which can lead to memory allocation overhead, especially for large arrays. * **`arrayA.push.apply(arrayB)`**: * Pros: More efficient in terms of memory usage since it modifies the original array instead of creating a new one. Can be faster than `concat()` due to less overhead from object creation and copying. * Cons: Less readable and more complex, especially for those unfamiliar with `push.apply()`. May not be as performant for very large arrays. **Library/Function used** In the provided benchmark, no external libraries are being used. However, if a library or function like Lodash was used instead of using built-in JavaScript functions for concatenation: * **`Lodash.concat()`**: Used for concatenating arrays, it's more readable and maintainable than the native `concat()` method. * **`Lodash.push.apply()`**: This might be less common as there is no equivalent to Lodash in this case. **Special JS feature/Syntax** The provided benchmark uses the following special JavaScript features: 1. **Template literals (`\r\n`) and multiline strings**: Used for formatting and readability. 2. **Arrow functions ( implicit, e.g., `let arrayB = new Array(500).fill('y',0,500);` )** : Not explicitly used but still applicable in the context of modern JavaScript. 3. **ES6+ syntax (`new Array()` constructor)**: Used to create arrays. Other alternatives for benchmarking JavaScript performance: 1. **Benchmarking libraries like Benchmark.js**: Specifically designed for measuring performance in JavaScript code, often includes a wide range of test cases and methods to compare different algorithms or approaches. 2. **Native `performance.now()`** : Simple and straightforward way to measure execution time in JavaScript. 3. **WebAssembly (WASM)**: For benchmarking CPU-intensive JavaScript code, especially for more low-level performance optimizations. Overall, these alternatives offer a range of options depending on the specific needs and goals of your benchmarking project.
Related benchmarks:
Array spread vs. push performance
Array push vs spread vs concat
push.apply vs concat
array update push vs spread vs concat
Array#concat vs Array#push
Comments
Confirm delete:
Do you really want to delete benchmark?