Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS: String '+' v.s. join
(version: 0)
Comparing performance of:
+ vs join
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var initialStr = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"; var initialSuffix = 123456789; var nLoops = 100;
Tests:
+
var str = ''; var suffix = initialSuffix; for (var i = 0; i < nLoops; i++, suffix *= 100) { str += initialStr + suffix; }
join
var stringArray = []; var suffix = initialSuffix; for (var i = 0; i < nLoops; i++, suffix *= 100) { stringArray.push(initialStr + suffix); } stringArray.join('');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
+
join
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/109.0
Browser/OS:
Firefox 109 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
+
83439.3 Ops/sec
join
48078.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The benchmark tests two approaches for concatenating strings in JavaScript: using the `+` operator and using the `join()` method. **Script Preparation Code** The script preparation code defines the following variables: * `initialStr`: a string containing a large set of characters repeated 26 times * `initialSuffix`: an initial suffix value used to increment the concatenation result * `nLoops`: the number of iterations for each test case (100) **Test Cases** There are two test cases: 1. **`+`**: This test case uses a simple loop to concatenate `initialStr` with `initialSuffix` using the `+` operator. 2. **`join()`**: This test case creates an array of strings by concatenating `initialStr` with `initialSuffix` and then calls the `join()` method on the array with an empty string as the separator. **Library Used** In this benchmark, no external library is used. The `+` operator and the `join()` method are built-in JavaScript functions. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax used in this benchmark. It only uses standard JavaScript syntax for concatenation and array manipulation. **Options Compared** The two test cases compare the performance of using the `+` operator versus the `join()` method for concatenating strings. **Pros and Cons of Each Approach** * **Using the `+` operator:** + Pros: - Simple and easy to understand - Fast execution + Cons: - Can be slow for large strings due to string duplication - May lead to memory issues if not done correctly (e.g., using `String.prototype.repeat()` instead of repeating the entire string) * **Using the `join()` method:** + Pros: - Fast execution - Reduces memory overhead compared to concatenating strings directly + Cons: - May have a higher overhead due to creating an array and calling `join()` - Less intuitive for some developers **Other Considerations** * The use of `initialSuffix` is likely used to increment the concatenation result, making it more comparable between test cases. * Using `String.prototype.repeat()` instead of repeating the entire string could improve performance but may not be supported in older browsers. **Alternative Approaches** Some alternative approaches for concatenating strings include: * Using `StringBuilder` or `TextBlob`: These libraries provide a more efficient way to concatenate strings, especially for large amounts. * Using `Array.prototype.concat()`: This method can be used instead of `join()` if the separator is not empty. * Using template literals (introduced in ECMAScript 2015): Template literals provide a concise way to concatenate strings with variables. Keep in mind that these alternatives may have different performance characteristics and are more complex than using the `+` operator or `join()`.
Related benchmarks:
String Concatenation
string concat speed
string concat speed
Split vs jin
replace vs split/join by Alex
Comments
Confirm delete:
Do you really want to delete benchmark?