Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
arr unshift vs push + reverse (small array) + concat
(version: 0)
Comparing performance of:
push + reverse vs concat vs concat2
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
push + reverse
var arr = [10]; for (let i = 0; i < 10; i++){ arr.push(i); } arr.reverse(); let str = arr.join(".")
concat
let str for (let i = 0; i < 10; i++){ str = `${i}.${str}`; }
concat2
let str for (let i = 0; i < 10; i++){ str = i+'.'+str }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
push + reverse
concat
concat2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
push + reverse
4286661.0 Ops/sec
concat
9103970.0 Ops/sec
concat2
9584831.0 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 measures the performance of three different approaches to concatenate strings: 1. **push + reverse**: This approach involves using `arr.push(i)` to append elements to an array and then reversing the array with `arr.reverse()`. The resulting string is created by joining the array elements with a dot (`.`) using `arr.join(".". )`. 2. **concat**: This approach uses the `+` operator to concatenate strings directly. It creates a single string variable `str` and appends each number to it, updating the value of `str` on each iteration. 3. **concat2**: This approach is similar to `concat`, but uses template literals (the `${}` syntax) to create the string. **Options compared** The benchmark compares the performance of these three approaches: * **push + reverse**: Uses array operations and reversal to build a string * **concat**: Uses direct string concatenation using the `+` operator * **concat2**: Uses template literals to create a string **Pros and cons of each approach:** 1. **push + reverse**: * Pros: + Can be more efficient for large arrays due to caching benefits in modern engines + Allows for easier handling of edge cases (e.g., null or undefined values) * Cons: + Requires explicit reversal, which can lead to performance overhead + May not be as readable or intuitive for some developers 2. **concat**: * Pros: + Simple and straightforward approach + Does not require explicit reversal or array manipulation * Cons: + Can lead to performance issues due to repeated string creations and allocations + May lead to stack overflow errors if the number of concatenations is extremely high 3. **concat2** (template literals): * Pros: + More readable and intuitive than direct string concatenation + Reduces memory allocation and deallocation overhead due to lazy evaluation * Cons: + May not be supported by older browsers or engines + Can lead to slower performance due to the complexity of template literals **Library usage** None of the benchmark test cases use a JavaScript library. **Special JS features or syntax** No special JavaScript features or syntax are used in these test cases. They all rely on standard JavaScript language constructs. **Other alternatives** If you were considering alternative approaches for string concatenation, some options might include: 1. **StringBuilder**: A custom class that provides efficient string manipulation methods. 2. **String.prototype.repeat()**: Used to repeat a string, which can be more efficient than concatenating strings using `+`. 3. **Internationalization APIs**: Some browsers provide internationalization APIs (e.g., `Intl` in modern Chrome versions) for working with Unicode strings. These alternatives might not be relevant to this specific benchmark, but they demonstrate the variety of approaches available for string concatenation in JavaScript.
Related benchmarks:
arr unshift vs push + reverse (large array)
myarr unshift vs push + reverse (small array)
myarr unshift vs push + reverse (small array)2
arr unshift vs push + reverse (small array) one item
arr unshift vs push + reverse vs concat case(small array)
Comments
Confirm delete:
Do you really want to delete benchmark?