Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String concatenation vs array join
(version: 5)
Comparing performance of:
String concatentation vs Array join vs Array join (w/ push)
Created:
8 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var str = ""; var i; var sArr = [];
Tests:
String concatentation
for (i = 1000; i > 0; i--) { str += "String concatenation. "; }
Array join
for (i = 1000; i > 0; i--) { sArr[i] = "String concatenation. "; } str = sArr.join("");
Array join (w/ push)
for (i = 1000; i > 0; i--) { sArr.push("String concatenation. "); } str = sArr.join("");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
String concatentation
Array join
Array join (w/ push)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String concatentation
3143.8 Ops/sec
Array join
9767.4 Ops/sec
Array join (w/ push)
21.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The benchmark, as defined in the provided JSON, tests three different approaches for concatenating strings: using string concatenation (`str +=`), array join (`sArr.join()`), and array push with subsequent join (`sArr.push()`). **Comparison of Approaches** 1. **String Concatenation**: This approach involves appending a string to another string using the `+=` operator. * Pros: + Simple and straightforward. + Fast, since it avoids creating new strings. * Cons: + Can lead to memory fragmentation and performance issues in JavaScript engines due to frequent string reallocations. 2. **Array Join**: This approach involves storing individual strings in an array and then joining them using the `join()` method. * Pros: + More efficient than string concatenation for large numbers of strings, since it avoids creating new strings and only creates a single string object. * Cons: + Requires creating an array and populating it with individual strings, which can be slower than string concatenation. 3. **Array Push with Join**: This approach is similar to the previous one, but instead of storing the individual strings in an array and then joining them, it pushes new strings onto the end of an existing array. * Pros: + Similar efficiency benefits as array join, since it avoids creating new strings. * Cons: + Requires modifying the original array, which can be slower than string concatenation. **Library Used: None** The benchmark does not use any external libraries or dependencies other than JavaScript's built-in `Array` and `String` objects. **Special JS Features/Syntax: None** There are no special JavaScript features or syntax used in this benchmark that would require specific knowledge of the language to understand. **Other Alternatives** Alternative approaches to string concatenation could include: * Using a `StringBuilder` class, if available in the JavaScript engine being tested. * Using a template literal (e.g., `${}`), which can provide a more efficient way to concatenate strings than traditional string concatenation. * Using a third-party library like Lodash or Underscore.js, which provides utility functions for string manipulation. However, these alternatives may not be supported by all JavaScript engines or platforms being tested in the benchmark.
Related benchmarks:
String concatenation vs array join precise
String concatenation vs array join preciselarge
String concatenation vs array join v6
String concatenation vs array join [previous author fucked up]
string concat + join vs unshift + join
Comments
Confirm delete:
Do you really want to delete benchmark?