Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Str concat vs Array join to put strings together
(version: 1)
Comparing performance of:
Str concat vs Array join
Created:
7 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Tests:
Str concat
let str = ''; for (let i = 0; i < 1000; i += 1) { str += Math.floor(Math.random() * Date.now()).toString(36); } console.log(str);
Array join
let str = []; for (let i = 0; i < 1000; i += 1) { str.push(Math.floor(Math.random() * Date.now()).toString(36)); } console.log(str.join(''));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Str concat
Array join
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Str concat
4305.2 Ops/sec
Array join
6020.4 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 7 months ago):
The benchmark defined in the provided JSON tests the performance of two different string concatenation methods in JavaScript: direct string concatenation using the `+=` operator and joining an array using the `Array.join()` method. Here's a breakdown of the benchmark: ### Options Compared 1. **String Concatenation (`Str concat`)**: - **Benchmark Definition**: The code initializes an empty string `str` and iteratively adds random strings to it using the `+=` operator. - **Execution**: ```javascript let str = ''; for (let i = 0; i < 1000; i += 1) { str += Math.floor(Math.random() * Date.now()).toString(36); } console.log(str); ``` 2. **Array Join (`Array join`)**: - **Benchmark Definition**: This option initializes an empty array `str` and pushes random strings into it. After the loop, it joins the elements of the array into a single string. - **Execution**: ```javascript let str = []; for (let i = 0; i < 1000; i += 1) { str.push(Math.floor(Math.random() * Date.now()).toString(36)); } console.log(str.join('')); ``` ### Performance Results Based on the latest benchmark results, the `Array join` method performs better, achieving approximately **6020.44 executions per second** compared to the **4305.18 executions per second** for `Str concat`. This indicates a noticeable performance advantage for the array joining approach in this specific scenario. ### Pros and Cons #### String Concatenation (`Str concat`): - **Pros**: - Simplicity: The syntax is straightforward and easy to understand. - Less code overhead: Directly concatenating strings may appear cleaner for smaller scales. - **Cons**: - Performance degradation: Using `+=` repeatedly creates new strings with each concatenation, leading to higher memory overhead and slower execution as the string grows in size. #### Array Join (`Array join`): - **Pros**: - Improved performance: Especially beneficial for concatenating many strings, as it minimizes memory allocation compared to repeated mutations of the string. - More optimal for larger datasets: Uses an array to gather parts and joins them at once, which is more efficient. - **Cons**: - Additional complexity: Slightly more complex to implement compared to simple string concatenation. ### Other Considerations and Alternatives 1. **Template Literals**: One option not tested here is the use of ES6 template literals. Although it provides a readable way to concatenate strings, in terms of performance, using an array join or simple concatenation may yield better results depending on the context and volume of strings. 2. **StringBuilder**: In languages that support it (like Java), a StringBuilder often provides better performance for constructing strings. In JavaScript, sticking to arrays with joins is the closest approach. 3. **Reduce and Map Functions**: Alternatives using functional programming (like `Array.reduce()`) can also concatenate strings but may not be as performant as the array join method tested here. ### Conclusion The benchmark effectively illustrates the performance differences between two common methods for string concatenation. Opting for the `Array.join()` method is generally recommended for scenarios involving multiple string concatenations, especially when performance is a critical factor, as demonstrated by the results obtained in this benchmark.
Related benchmarks:
string_concat_19oq287398127398127398123
String.prototype.concat vs Array.prototype.join
String concatenation vs array join 2
String concatenation vs concat method
String += with another string vs pushing into an array and array join in the end
string concatenation vs array acc
string concat and array.join
string concat and array.join v2
string concat vs .toString()
Comments
Confirm delete:
Do you really want to delete benchmark?