Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Native JS: Array.join strings vs template literals vs String.concat vs string sum
(version: 1)
find the best solution for concatenate strings
Comparing performance of:
join vs concat vs using template literals vs string sum
Created:
11 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
var name = "name"; var id = "id";
Tests:
join
for (let i = 0; i < 80; ++i) { let result = [id, ':', i, ', ', name, ':', 'name', i].join(); }
concat
for (let i = 0; i < 80; ++i) { let result = "".concat(id, ':', i, ', ', name, ':', 'name', i); }
using template literals
for (let i = 0; i < 80; ++i) { let result = `${id}:${i}, ${name}:name${i}`; }
string sum
for (let i = 0; i < 80; ++i) { let result = id + ':' + i + ', ' + name + ':' + 'name' + i }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
join
concat
using template literals
string sum
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
12 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
join
69717.1 Ops/sec
concat
104490.2 Ops/sec
using template literals
186737.4 Ops/sec
string sum
192308.4 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 11 months ago):
This benchmark focuses on evaluating different methods for concatenating strings in JavaScript, specifically comparing four approaches: using `Array.join`, `String.concat`, template literals, and the string sum operator (`+`). ### Approach Comparisons 1. **Using `Array.join`**: - **Benchmark Code**: `let result = [id, ':', i, ', ', name, ':', 'name', i].join();` - **Pros**: - It allows for easy handling of multiple strings and can work well with dynamic array content. - The syntax is clear when concatenating large numbers of strings. - **Cons**: - Involves creating an array first, which may introduce additional overhead for performance as it requires memory allocation. 2. **Using `String.concat`**: - **Benchmark Code**: `let result = "".concat(id, ':', i, ', ', name, ':', 'name', i);` - **Pros**: - Explicitly shows the intention of concatenating strings. - Can be clearer than the `+` operator in some contexts. - **Cons**: - Generally, performance may not be as good as other methods due to its non-optimized path in some JavaScript engines. 3. **Using Template Literals**: - **Benchmark Code**: `let result = `${id}:${i}, ${name}:name${i}`;` - **Pros**: - Provides a very readable syntax and is easy to write. - Allows for multi-line strings without needing to use escape characters. - **Cons**: - Older browsers that do not support ES6 may not handle this syntax. However, modern environments usually support it. 4. **Using String Sum (`+`)**: - **Benchmark Code**: `let result = id + ':' + i + ', ' + name + ':' + 'name' + i;` - **Pros**: - Very straightforward and widely understood syntax. - Works in virtually all JavaScript environments. - **Cons**: - As the number of strings increases, it can become cumbersome to read and maintain compared to more structured approaches like template literals. ### Benchmark Results According to the benchmark results, the execution speeds (measured as executions per second) for each method were: 1. **Template Literals**: 125684.12 2. **String Sum (`+`)**: 125207.22 3. **String.concat**: 83752.67 4. **Array.join**: 82466.44 ### Considerations - **Performance Implications**: If string concatenation is a performance-critical aspect of your application, the results suggest that template literals might be the best option, followed closely by using the `+` operator. - **Readability vs. Performance**: While performance is essential, code clarity and maintainability should not be ignored. Template literals score high on readability. - **Browser Compatibility**: It is important to consider the target audience and their browsers since older environments might not support newer features like template literals. ### Alternatives - **String Builder Libraries**: For complex scenarios, especially where concatenation happens in large loops, you can utilize libraries optimized for string building. - **Using Faster Algorithms**: If concatenation occurs in numerous iterations, it may be worthwhile to consider algorithms that are optimized for building longer strings rather than typical concatenation methods. This benchmark helps in understanding the trade-offs between performance and readability in string concatenation, guiding developers in choosing the right method based on their specific needs.
Related benchmarks:
Native JS: concatenate string with + vs template literals vs String.concat
Native JS: concatenate string with + vs template literals vs String.concat vs array join
Native JS: concatenate string with + vs template literals vs String.concat - My test
Native JS: concatenate string with + vs String.concat
Native JS: Array.join strings vs template literals vs String.concat
Native JS: concatenate string with + vs template literals vs String.concat edit
Native JS: concatenate string with + vs template literals vs String.concat edit 2 concat 80
Native JS: concatenate string with + vs template literals vs String.concat + numeric hash
shvaer Native JS: concatenate string with + vs template literals vs String.concat
Comments
Confirm delete:
Do you really want to delete benchmark?