Engineering Core
ISB Vietnam's skilled software engineers deliver high-quality applications, leveraging their extensive experience in developing financial tools, business management systems, medical technology, and mobile/web platforms.

We've all been there. You need to generate a large report. This might be a massive CSV file, a sprawling JSON for an API, or a huge chunk of HTML. At the heart of these tasks is a simple operation: string concatenation.

But things change when "big data" enters the picture. The way you join your strings can mean the difference between a snappy app and a slow one.

Why does this matter? Imagine generating a CSV with a million rows. If each concatenation is inefficient, that inefficiency multiplies a million times. This leads to slow performance and high memory use.

Specifically, some common scenarios for this include:

  • CSV Generation: Exporting thousands or millions of records to a file.
  • JSON Serialization: Building large JSON objects to send over the network.
  • XML/HTML Generation: Creating large XML documents or rendering HTML on a server.
  • Log File Creation: Combining many log entries into a single file.
  • Building Complex Query Strings: Constructing long and intricate queries for a database.

Therefore, how do we tackle string concatenation effectively in JavaScript? Let's explore the common methods.

JavaScript offers several ways to combine strings. Each has its place, but their performance characteristics vary significantly when dealing with many concatenations.

 

I. The + Operator (Addition/Concatenation Operator)

1. How it works

const str1 = "ISB";
const str2 = "Viet Nam";
newString = str1 + " " + str2; //OUTPUT: ISB Viet Nam

2. Advantages

  • Simple, intuitive, and widely known.
  • For a small number of strings, it's often very fast.

3. Disadvantages

  • However, it can be a performance trap. Using it repeatedly in a loop is inefficient. This is because strings in JavaScript are immutable. Each += operation creates a new string in memory. It copies the old string and adds the new piece. For large strings, this copying becomes very expensive.

4. A Simple Speed Test

  • Example:
const iterations = 1000000; // Number of pieces to concatenate
const piece = "abc"; // The small string piece to append
console.time("Plus Operator");
let strPlus = "";
for (let i = 0; i < iterations; i++) {
   strPlus += piece;
}
console.timeEnd("Plus Operator");
  • Output:
Plus Operator: 60.948974609375 ms

 

II. String.prototype.concat() Method

1. How it works

const str1 = "ISB";
const str2 = "Viet Nam";
newString = str1.concat(" ", str2); //OUTPUT: ISB Viet Nam

2. Advantages

  • It was made specifically for string concatenation.
  • It can also take multiple arguments at once.

3. Disadvantages

  • It performs similarly to the + operator. In other words, it also creates new strings with each use. This makes it slow for many appends in a loop.
  • Can be slightly more verbose than + for simple cases.

4. A Simple Speed Test

  • Example:
const iterations = 1000000; // Number of pieces to concatenate
const piece = "abc"; // The small string piece to append
console.time("String.concat()");
let strConcat = "";
for (let i = 0; i < iterations; i++) {
   strConcat = strConcat.concat(piece);
}
console.timeEnd("String.concat()");
  • Output:
String.concat(): 71.370849609375 ms

 

III. Template Literals (Backticks)

1. How it works

const str1 = "ISB"; 
const str2 = "Viet Nam";
newString = `${str1} ${str2}`; //OUTPUT: ISB Viet Nam

2. Advantages

  • They offer excellent readability, especially for adding variables.
  • Their performance is generally good and often on par with the + operator.

3. Disadvantages

  • On the other hand, using them naively in a loop can be slow. A loop like bigString = `${bigString}${nextPiece}`; suffers from the same problems as the + operator.
  • They are designed for formatting, not for building massive strings piece by piece.

4. A Simple Speed Test

  • Example:
const iterations = 1000000; // Number of pieces to concatenate
const piece = "abc"; // The small string piece to append
console.time("Template Literals (Naive Accumulation)");
let strTemplate = "";
for (let i = 0; i < iterations; i++) {
   strTemplate = `${strTemplate}${piece}`;
}
console.timeEnd("Template Literals (Naive Accumulation)");
  • Output:
Template Literals (Naive Accumulation): 75.455810546875 ms

 

IV. Array.prototype.join('') Method

1. How it works

const str1 = "ISB";
const str2 = "Viet Nam";
var parts = [];
parts.push(str1);
parts.push(" ");
parts.push(str2);
newString = parts.join(''); //OUTOUT: ISB Viet Nam

2. Advantages

  • This method is the performance champion for many appends. It shines when building a large string from many smaller pieces. Pushing strings into an array is efficient. The final join('') operation is highly optimized to create the final string all at once.
  • As a result, it avoids creating many intermediate strings.

3. Disadvantages

  • It is slightly more verbose for simple tasks.
  • It also has the overhead of creating and managing an array.

4. A Simple Speed Test

  • Example:
const iterations = 1000000; // Number of pieces to concatenate
const piece = "abc"; // The small string piece to append
console.time("Array.join()");
const arrJoin = [];
for (let i = 0; i < iterations; i++) {
   arrJoin.push(piece);
}
const finalStrJoin = arrJoin.join('');
console.timeEnd("Array.join()");
  • Output:
Array.join(): 30.193115234375 ms

 

V. Conclusion: Choose Wisely for Peak Performance

The results are clear. Array.join('') is much faster when building large strings. The other methods perform worse. This is due to the overhead of creating many intermediate strings.

In short, Array.join('') does all the work at the very end. It performs a single, optimized operation. This approach is much better for the JavaScript engine and for memory management.

While Array.join('') is the performance king, it isn't always the best solution.

  • For example, with a few, simple concatenations: const greeting = "Hello, " + name + "!"; or const greeting = `Hello, ${name}!`; is perfectly fine and often more readable. The performance difference here is negligible.
  • Furthermore, When readability with embedded expressions is key: Template literals are fantastic: const message = Item: ${item.name}, Price: `${item.price.toFixed(2)}`;. Even if you build an array of these formatted strings and then join, template literals are great for creating the pieces.

Ultimately, By choosing the right tool for the job, you write code that is both highly performant and easy to maintain.

 

Whether you need scalable software solutions, expert IT outsourcing, or a long-term development partner, ISB Vietnam is here to deliver. Let’s build something great together—reach out to us today. Or click here to explore more ISB Vietnam's case studies.

 

※ References:

(1) Official Array.join() documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

(2) Why string loops are slow: https://stackoverflow.com/questions/18561424/using-for-strings-in-a-loop-is-it-bad-practice

(3) Three ways to concatenate strings: https://www.freecodecamp.org/news/how-js-string-concatenation-works/

(4) Image source: https://www.pexels.com/search/Coding/

Written by
Author Avatar
Engineering Core
ISB Vietnam's skilled software engineers deliver high-quality applications, leveraging their extensive experience in developing financial tools, business management systems, medical technology, and mobile/web platforms.

COMPANY PROFILE

Please check out our Company Profile.

Download

COMPANY PORTFOLIO

Explore my work!

Download

ASK ISB Vietnam ABOUT DEVELOPMENT

Let's talk about your project!

Contact US