Seems based on benchmarks at JSPerf that using += is the fastest method, though not necessarily in every browser.

For building strings in the DOM, it seems to be better to concatenate the string first and then add to the DOM, rather then iteratively add it to the dom. You should benchmark your own case though.

(Thanks @zAlbee for correction)

Answer from Jakub Hampl on Stack Overflow
🌐
SitePoint
sitepoint.com › blog › javascript › high-performance string concatenation in javascript
High-performance String Concatenation in JavaScript — SitePoint
November 5, 2024 - There are a number of ways to concatenate strings in JavaScript: str = "a" + "b"; str += "c"; str = str.concat("d", "e"); ... If you’re only joining a few strings, you should use whichever method is most practical. Performance gains or losses will be negligible in all browsers.
🌐
freeCodeCamp
forum.freecodecamp.org › t › which-is-more-efficient-str-some-string-or-array-push-join › 5802
Which is more efficient str += "some string" or array.push().join()
May 29, 2016 - I know String in JavaScript is immutable which means if I concatenate strings by using +=, a new string will be created with its content being copied from the old string, the old string then gets garbage collected, but …
🌐
Medium
medium.com › @zhongdongy › the-performance-of-javascript-string-concat-e52466ca2b3a
The Performance of JavaScript String Concat | by Zhongdong Yang | Medium
October 17, 2017 - From the table, we can find that both the loop time and single string length don’t affect execution duration. The only factor is concat times. When there are no more than 100 thousand concat, a direct ‘+=’ operation takes less time. When there are more than 1 million concat, use join() is definitely better, while use array index to extend elements is even better. ... So, within 413,085 times of concat, using ‘+=’ has the same performance as using join() does.
🌐
Useaxentix
useaxentix.com › blog › javascript › concatenate-strings-in-javascript
Concatenate Strings in JavaScript: Complete Guide
Master JavaScript string concatenation with the + operator, template literals, concat(), and join() methods. Learn performance tips, best practices, and real-world examples for efficient string operations.
Top answer
1 of 4
73

Consider this piece of JavaScript code:

var a = 10;
var b = 20;
console.log('result is ' + a + b);

This will log

result is 1020

Which most likely is not what was intended, and can be a hard to track bug.

2 of 4
49

When you say "bad" do you mean "incorrect" or do you mean "slow"? The argument about using mathematical operators to do string concatenation is arguably an "incorrect" argument, but there's also an argument to be made that using + to do a lot of string concatenation can be very slow.

We're not talking about "Hello" + number when we talk about performance, we're talking about building up a relatively large string by repeatedly appending to it in a loop.

var combined = "";
for (var i = 0; i < 1000000; i++) {
    combined = combined + "hello ";
}

In JavaScript (and C# for that matter) strings are immutable. They can never be changed, only replaced with other strings. You're probably aware that combined + "hello " doesn't directly modify the combined variable - the operation creates a new string that is the result of concatenating the two strings together, but you must then assign that new string to the combined variable if you want it to be changed.

So what this loop is doing is creating a million different string objects, and throwing away 999,999 of them. Creating that many strings that are continually growing in size is not fast, and now the garbage collector has a lot of work to do to clean up after this.

C# has the exact same problem, which is solved in that environment by the StringBuilder class. In JavaScript, you'll get much better performance by building up an array of all the strings you want to concatenate, and then joining them together one time at the end, instead of a million times in the loop:

var parts = [];
for (var i = 0; i < 1000000; i++) {
    parts.push("hello");
}
var combined = parts.join(" ");
🌐
freeCodeCamp
freecodecamp.org › news › efficient-string-building-in-javascript
How to Work with Strings in JavaScript – Tips for Efficient String Concatenation
November 29, 2022 - As you can see, the same result was achieved in 8 seconds, which is 10 times faster than regular string concatenation. As you can see, by changing just three lines of code, you can significantly increase the performance of your data processing ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › concat
String.prototype.concat() - JavaScript | MDN
A new string containing the combined text of the strings provided. The concat() function concatenates the string arguments to the calling string and returns a new string.
Find elsewhere
🌐
Google
docs.google.com › document › d › 1o-MJPAddpfBfDZCkIHNKbMiM86iDFld7idGbNQLuKIQ › preview
Fast string concatenation in JavaScript - Google Docs
Fast string concatenation in JavaScript Attention: Shared Google-externally Author: bmeurer@ Last Updated: 2019-03-21 TL;DR Fast String concatenation is hard in JavaScript. Engines have plenty of clever optimizations in place that make it non-trivial to find the best approach. This document outli...
Top answer
1 of 11
155

Browser string optimizations have changed the string concatenation picture.

Firefox was the first browser to optimize string concatenation. Beginning with version 1.0, the array technique is actually slower than using the plus operator in all cases. Other browsers have also optimized string concatenation, so Safari, Opera, Chrome, and Internet Explorer 8 also show better performance using the plus operator. Internet Explorer prior to version 8 didn’t have such an optimization, and so the array technique is always faster than the plus operator.

— Writing Efficient JavaScript: Chapter 7 – Even Faster Websites

The V8 javascript engine (used in Google Chrome) uses this code to do string concatenation:

// ECMA-262, section 15.5.4.6
function StringConcat() {
  if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
    throw MakeTypeError("called_on_null_or_undefined", ["String.prototype.concat"]);
  }
  var len = %_ArgumentsLength();
  var this_as_string = TO_STRING_INLINE(this);
  if (len === 1) {
    return this_as_string + %_Arguments(0);
  }
  var parts = new InternalArray(len + 1);
  parts[0] = this_as_string;
  for (var i = 0; i < len; i++) {
    var part = %_Arguments(i);
    parts[i + 1] = TO_STRING_INLINE(part);
  }
  return %StringBuilderConcat(parts, len + 1, "");
}

So, internally they optimize it by creating an InternalArray (the parts variable), which is then filled. The StringBuilderConcat function is called with these parts. It's fast because the StringBuilderConcat function is some heavily optimized C++ code. It's too long to quote here, but search in the runtime.cc file for RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderConcat) to see the code.

2 of 11
26

Firefox is fast because it uses something called Ropes (Ropes: an Alternative to Strings). A rope is basically just a DAG, where every Node is a string.

So for example, if you would do a = 'abc'.concat('def'), the newly created object would look like this. Of course this is not exactly how this looks like in memory, because you still need to have a field for the string type, length and maybe other.

a = {
 nodeA: 'abc',
 nodeB: 'def'
}

And b = a.concat('123')

b = {
  nodeA: a, /* {
             nodeA: 'abc',
             nodeB: 'def'
          } */
  nodeB: '123'
}           

So in the simplest case the VM has to do nearly no work. The only problem is that this slows down other operations on the resulting string a little bit. Also this of course reduces memory overhead.

On the other hand ['abc', 'def'].join('') would usually just allocate memory to lay out the new string flat in memory. (Maybe this should be optimized)

🌐
Modus Create
moduscreate.com › home › javascript performance tips & tricks
JavaScript Performance Tips & Tricks - Modus Create
May 22, 2020 - Update: After further examination, String.prototype.concat is faster with lower number of arguments (especially faster with just one argument). It is also faster in general in OSX Safari and iOS Safari.
🌐
Technical Feeder
technicalfeeder.com › 2021 › 07 › string-concat-in-javascript-performance-comparison
String concat in JavaScript Performance Comparison | Technical Feeder
May 6, 2022 - Array.prototype.join function is the slowest to concatenate string. Others are almost the same performance. In most cases, we don’t have to take care of which one to use.
🌐
GitHub
gist.github.com › Chunlin-Li › c49d6be2e9179cc26c95
node / javascript performance: string concat operator (+) vs array.join() · GitHub
node / javascript performance: string concat operator (+) vs array.join() Raw · 11261409.md · string concatenate operator, for short string but large times. array join(), for short string but large times. node version : v4.1.2 platform: Intel I7, 16G DDR3, Ubuntu x64 ·
🌐
DEV Community
dev.to › efpage › the-fantastic-speed-of-template-literals-4f0p
The fantastic speed of template literals... - DEV Community
May 28, 2023 - Now, the time increases from 150 ms to about 200 ms for 1 Mio operations, so one concatenation takes 0,05 Microseconds. This is identical for both approaches. Anyway, the performance is increadibly.
🌐
Medium
medium.com › @ryan_forrester_ › string-concatenation-in-javascript-a-comprehensive-guide-fa3f46e6a227
String Concatenation in JavaScript: A Comprehensive Guide | by ryan | Medium
March 2, 2025 - Using the join() method on an array can enhance performance when concatenating multiple strings, especially in loops. String concatenation plays a vital role in various JavaScript applications.
🌐
GitHub
github.com › airbnb › javascript › issues › 40
String concat speed · Issue #40 · airbnb/javascript
January 11, 2013 - In the Strings section the standard is: "Strings longer than 80 characters should be written across multiple lines using string concatenation." I understand the clarity of code - but the performance is 80-100% worse. jsPerf The need for ...
Published   Jan 11, 2013