Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Native JS: concatenate string with + vs template literals vs String.concat
(version: 0)
find best solution for concatenate 4 strings
Comparing performance of:
using plus operator vs using concat function vs using template literals
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var name = "name"; var id = "id";
Tests:
using plus operator
for (let i = 0; i < 80; ++i) { let result = id + ": 1, " + name + ": someItem"; }
using concat function
for (let i = 0; i < 80; ++i) { let result = "".concat(id, ": 1, ", name, ": someItem"); }
using template literals
for (let i = 0; i < 80; ++i) { let result = `${id}: 1, ${name}: someItem`; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
using plus operator
using concat function
using template literals
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 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
using plus operator
142259.9 Ops/sec
using concat function
102228.9 Ops/sec
using template literals
139364.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation. **Benchmark Purpose and Comparison** The provided benchmark measures the performance of three different methods to concatenate strings in JavaScript: 1. Using the `+` operator 2. Using the `concat()` function 3. Using template literals (`${}`) The goal is to find the best solution for concatenating four strings, which is a common operation in many JavaScript applications. **Pros and Cons of Each Approach** 1. **Using the `+` operator**: This method is simple and widely supported, but it can lead to performance issues due to string concatenation creating new objects each time. It's also slower than other methods. 2. **Using the `concat()` function**: The `concat()` function is a built-in method that creates a new object by concatenating multiple strings. While it's slightly faster than using the `+` operator, it still involves creating and garbage-collecting objects, which can be slower. 3. **Using template literals**: Template literals are a modern feature introduced in ECMAScript 2015 (ES6). They provide a more concise and readable way to concatenate strings while avoiding the need for explicit concatenation methods. Template literals have several advantages: * They avoid creating intermediate objects or using explicit concatenation methods. * They provide better readability and maintainability. * They're supported by most modern browsers, including Firefox 131. However, template literals might not be supported in older browsers, which could affect the benchmark results. **Library Usage** None of the test cases use any libraries explicitly. However, the `concat()` function is a built-in method that uses JavaScript's string concatenation mechanism under the hood. **Special JS Features or Syntax** The benchmark uses the `let` keyword for variable declarations (introduced in ECMAScript 2015), which is not a special feature per se but has become increasingly common in modern JavaScript code. Additionally, the use of arrow functions (`=>`) is not relevant to this specific benchmark. **Other Alternatives** If you wanted to explore other alternatives, you could consider: * Using `String.join()` method (introduced in ECMAScript 2009) * Using a custom function or module for string concatenation * Using a library like Underscore.js or Lodash for string manipulation However, these alternatives are not part of the original benchmark and would likely change the outcome of the performance comparison.
Related benchmarks:
Native JS: concatenate string with + vs template literals vs String.concat - My test
Native JS: concatenate string with + vs template literals vs String.concat_0
plus vs template literals vs String.concat
Native JS: concatenate string with + vs template literals vs String.concat + numeric hash
Native JS2: concatenate string with + vs template literals vs String.concat
Comments
Confirm delete:
Do you really want to delete benchmark?