Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String addition + vs constructor vs template
(version: 1)
Comparing performance of:
Addition vs Constructor vs template
Created:
10 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
let a = 1;
Tests:
Addition
let b = "a" + a + "c";
Constructor
let b = String("a", a, "c");
template
let b = `a${a}c`;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Addition
Constructor
template
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0
Browser/OS:
Chrome 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Addition
150616480.0 Ops/sec
Constructor
96341584.0 Ops/sec
template
137915744.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 10 months ago):
In this benchmark, three different methods of string manipulation in JavaScript are tested to compare their performance: string addition (concatenation), the String constructor, and template literals. ### Options Compared: 1. **String Addition**: ```javascript let b = "a" + a + "c"; ``` - **Description**: This approach uses the `+` operator to concatenate strings. It is a traditional method that has been in JavaScript since its inception. - **Performance**: In the results, this method showed the highest execution speed. - **Pros**: - Simplistic and straightforward syntax. - Highly familiar to most developers. - **Cons**: - May become less readable with complex expressions or multiple concatenations. 2. **String Constructor**: ```javascript let b = String("a", a, "c"); ``` - **Description**: This method uses the `String` constructor to create a new string. However, the incorrect use of this constructor suggests that it is generally less favored in this context. - **Performance**: This method demonstrated the slowest execution speed in the results. - **Pros**: - Can be used to convert non-string values to strings. - **Cons**: - Less intuitive for concatenation purposes; it might confuse those who expect a single argument behavior. - Greater overhead compared to direct concatenation. - Not commonly used for creating strings via concatenation. 3. **Template Literals**: ```javascript let b = `a${a}c`; ``` - **Description**: This syntax utilizes template literals (enclosed in backticks) which allow for embedded expressions, making string interpolation seamless. - **Performance**: This method performed adequately but was slower than string addition. - **Pros**: - Improved readability, especially when dealing with complex strings or variables. - Allows multiline strings and easier expression embedding. - **Cons**: - Slightly more complex than basic string addition and not universally supported in very old environments. ### Performance Results: - **Highest Performance**: String addition (`ExecutionsPerSecond: 1,065,782,336.0`) - **Medium Performance**: Template literals (`ExecutionsPerSecond: 231,380,832.0`) - **Lowest Performance**: String constructor (`ExecutionsPerSecond: 73,928,680.0`) ### Other Considerations: - **Browser Compatibility**: All three methods are widely supported across modern browsers, but template literals are not available in very old browsers (pre-ES6). - **Readability vs. Performance**: While string addition is the fastest, template literals provide better readability for more complex strings. Developers should weigh the importance of code clarity against performance, especially in a context where string manipulation is conducted heavily. - **Alternatives**: Besides these methods, other alternatives exist in JavaScript like array `join('')` for complex concatenations or leveraging libraries like `lodash` for more advanced string manipulation features. In summary, while string addition is the most performant approach tested, template literals offer enhanced readability, making them the preferred choice in many coding scenarios. The String constructor, while valid, is more of an oddity when it comes to string concatenation in JavaScript.
Related benchmarks:
`a += b` vs `a = a + b`
addtion operator vs plus
concatenation vs template literal
Native JS: concatenate string with + vs template literals (2)
String vs. Template Literal
asdsasq
number to string: String() vs template
String vs. Template Literal with var
String vs. Template Literal with var 2
Comments
Confirm delete:
Do you really want to delete benchmark?