Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String concatenation-2021-2
(version: 0)
Comparing performance of:
Plus operator vs using concat function vs using template literals vs Plus equal operator
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var stringArray = [ "vsYjQhWW81jNQ7u", "9KQsDJfCnT4JJrf", "YMzWT1Uco2T9AB5", "0zIgDBXDMZdieUl", "4hItfhLfRD2hLIr", "XYw1Yn8SgaBzinu", "XTTahL0gbmFMdGn", "P37CWiCns1khHEC" ]; var result = "";
Tests:
Plus operator
for (var i = 0; i < stringArray.length; i++) { result = result + stringArray[i]; } result=result+".";
using concat function
for (var i = 0; i < stringArray.length; i++) { result = result.concat(stringArray[i]); } result = result.concat(".");
using template literals
for (var i = 0; i < stringArray.length; i++) { result = `${result}${stringArray[i]}`; } result = `${result}.`;
Plus equal operator
for (var i = 0; i < stringArray.length; i++) { result += stringArray[i]; } result+=".";
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Plus operator
using concat function
using template literals
Plus equal operator
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 and its test cases. **Benchmark Definition** The benchmark measures the performance of string concatenation in JavaScript. The script preparation code creates an array of strings (`stringArray`) and initializes an empty result variable (`result`). The HTML preparation code is not relevant to this benchmark, as it only defines a reference to the result string. **Test Cases** There are four test cases, each comparing a different approach to concatenate strings: 1. **Plus operator**: `result = result + stringArray[i]` * Pros: Simple and widely supported. * Cons: May be slower than other approaches due to the overhead of creating a temporary string object. 2. **Using concat function**: `result = result.concat(stringArray[i])` * Pros: Fast and efficient, as it avoids creating temporary string objects. * Cons: Requires the use of the `concat` method, which may not be immediately familiar to all developers. 3. **Using template literals**: `${result}${stringArray[i]}` * Pros: Modern and efficient, as it uses a new syntax for concatenating strings. * Cons: May require additional setup or configuration, as it's a relatively new feature in JavaScript. 4. **Plus equal operator**: `result += stringArray[i]` * Pros: Simple and widely supported, similar to the plus operator approach. * Cons: Similar to the plus operator approach, may be slower due to temporary string object creation. **Library/Features** There are no external libraries used in this benchmark. However, it's worth noting that modern JavaScript features like template literals were introduced in ECMAScript 2015 (ES6) and have become widely supported across most browsers. **Special JS Feature/Syntax** Template literals are a relatively new feature in JavaScript, introduced in ES6. They allow you to embed expressions inside string literals using the `${}` syntax. This approach is efficient because it avoids creating temporary string objects. The benchmark measures the performance of these four approaches on a mobile device running Chrome 90 and Android 11. The results show that the "using concat function" approach is the fastest, followed by the "+= operator", then the "Plus operator", and finally the "Using template literals". **Other Alternatives** Some alternative approaches to string concatenation in JavaScript include: * Using `Array.prototype.join()`: This method joins all elements of an array into a single string. * Using `String.prototype.repeat()`: This method repeats a string a specified number of times. * Using a custom loop and concatenating strings manually. These alternatives may have different performance characteristics compared to the approaches tested in this benchmark.
Related benchmarks:
String concatenation vs array join Performance:3
String concatenation-2021
array.join vs string literal vs string concatenation
String concatenation vs array join258
Comments
Confirm delete:
Do you really want to delete benchmark?