Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String concatenation v.s. ES6 template
(version: 4)
Comparing performance of:
Concatenation vs ES6 template vs Array join
Created:
9 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function concatenation(a, b, c) { return "a = " + a + ", b = " + b + ", c = " + c; } function template(a, b, c) { return `a = ${a}, b = ${b}, c = ${c}`; } var arrayTemplate = ["a = ", null, ", b = ", null, ", c = ", null]; function arrayJoin(a, b, c) { arrayTemplate[1] = a; arrayTemplate[3] = b; arrayTemplate[5] = c; return arrayTemplate.join(""); }
Tests:
Concatenation
for (var i = 0; i < 100; i++) { window.a = concatenation(i, "random", "text"); }
ES6 template
for (var i = 0; i < 100; i++) { window.a = template(i, "random", "text"); }
Array join
for (var i = 0; i < 100; i++) { window.a = arrayJoin(i, "random", "text") }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Concatenation
ES6 template
Array join
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **Benchmark Definition JSON** The benchmark is designed to compare three different approaches for string concatenation in JavaScript: the traditional `+` operator, ES6 template literals (`\${}`), and an array join approach. 1. **Traditional Concatenation**: The first script defines a function `concatenation(a, b, c)` that takes three arguments and returns a string by concatenating them using the `+` operator. 2. **ES6 Template Literals**: The second script defines a function `template(a, b, c)` that takes three arguments and returns a string using template literals (`\${}`). This allows for more readable and efficient string formatting. 3. **Array Join**: The third script uses an array to store the parts of the concatenated string and then joins them together using the `join()` method. **Options Compared** The benchmark compares the performance of these three approaches under different conditions: * Traditional concatenation * ES6 template literals * Array join **Pros and Cons of Each Approach** 1. **Traditional Concatenation** * Pros: widely supported, easy to understand, no additional dependencies required. * Cons: can lead to unexpected behavior when dealing with large strings or complex data types (e.g., null or undefined values). 2. **ES6 Template Literals** * Pros: more readable and efficient than traditional concatenation, supports formatting options like `${variable} ${unit}`. * Cons: requires modern browsers that support ES6+ syntax, may not work in older environments. 3. **Array Join** * Pros: allows for dynamic string construction using an array of parts, can be faster than traditional concatenation for large strings. * Cons: more complex and harder to understand than traditional concatenation or ES6 template literals. **Library Used** None explicitly mentioned, but the `join()` method used in the array join approach is a built-in JavaScript method that doesn't require any external libraries. **Special JS Feature or Syntax** The benchmark uses ES6+ syntax (template literals) and modern browser features (e.g., the `window` object). These are not special features per se, but rather modern JavaScript capabilities that may not be supported in older environments or browsers. **Alternative Approaches** Some alternative approaches for string concatenation in JavaScript include: * Using the `string.repeat()` method: `String('a' + 'b').repeat(2)` is equivalent to `"ab".concat("ab")`. * Utilizing a library like Underscore.js, which provides a more functional programming style for string manipulation. * Leveraging modern string interpolation libraries like LitTemplate or React's JSX. Keep in mind that the best approach often depends on the specific use case and requirements of your project.
Related benchmarks:
Native JS: concatenate string with + vs template literals (2)
template literal vs array.join vs string.concat vs the + operator
template literal vs array.join vs unallocated array.join vs string.concat vs the + operator
template literal vs allocated array.join vs unallocated array.join vs string.concat vs the + operator
Comments
Confirm delete:
Do you really want to delete benchmark?