What is String Concatenation and its performance impact?
Updated Apr 28, 2026
Short answer
String concatenation is the process of joining two or more strings to create a new string. Its performance depends on the programming language and string implementation, but in many languages strings are immutable, meaning each concatenation creates a new string and copies existing characters. Repeated concatenation in a loop can therefore become inefficient, often resulting in O(N²) time complexity.
Deep explanation
String concatenation combines multiple strings into a single string.
For example:
first = "Hello"second = "World"
result = first + " " + secondThe result is:
Hello WorldThe performance impact comes from how strings are stored and modified internally.
Immutable Strings
In many programming languages, strings are immutable. This means once a string is created, it cannot be changed.
For example:
s = "Hello"s = s + " World"The original string "Hello" is not modified. Instead, a new string is created:
Old string:Hello
New string:Hello WorldThe characters from the old string must be copied into the new memory location.
If a single concatenation involves:
- String
Aof lengthM - String
Bof lengthN
then the operation requires copying M + N characters:
Time complexity = O(M + N)Repeated Concatenation Problem
A common performance issue occurs when repeatedly appending strings in a loop.
Example:
result = ""
for word in words: result = result + wordSuppose there are N words and each word has length L.
Each iteration creates a new string and copies all existing characters:
Iteration 1: copy L charactersIteration 2: copy 2L charactersIteration 3: copy 3L characters...Iteration N: copy NL charactersThe total work becomes:
L + 2L + 3L + ... + NL
= L * (1 + 2 + 3 + ... + N)
= O(N²L)This can become very slow for large amounts of text.
Using String Builders
Many languages provide mutable string-building utilities that avoid repeated copying.
Examples:
- Python:
listwithjoin() - Java:
StringBuilder - C#:
StringBuilder - C++:
std::stringwith efficient append operations
Example in Python:
words = ["Hello", " ", "World"]
result = "".join(words)The strings are collected first, then combined in one operation.
The complexity becomes:
O(total length of all strings)instead of repeatedly reallocating memory.
Common Concatenation Approaches
| Approach | Typical Performance |
|---|---|
| Concatenating a few strings | Usually efficient enough |
Repeated + inside a loop | Can become O(N²) |
| String builder / buffer | Usually O(N) |
| Joining a collection of strings | Usually O(N) |
Language-Specific Nuance
The exact behavior depends on compiler and runtime optimizations.
For example:
- Some languages optimize consecutive string literals:
message = "Hello " "World"- Some runtimes optimize simple concatenation cases.
- However, developers should still avoid repeated concatenation in large loops unless the language guarantees efficient behavior.
Real-world example
Building a large report, log file, or HTML document by repeatedly appending strings can cause unnecessary memory allocation.
Inefficient approach:
report = ""
for line in lines: report += line + "\n"A better approach:
lines_with_breaks = []
for line in lines: lines_with_breaks.append(line + "\n")
report = "".join(lines_with_breaks)The second approach creates the final string once instead of repeatedly creating intermediate strings.
This matters when generating large outputs such as:
- Export files
- API responses
- Generated HTML pages
- Application logs
Common mistakes
- * Assuming string concatenation is always `O(1)` because adding one string looks like a simple operation.
- * Ignoring string immutability when analyzing performance.
- * Using repeated string concatenation inside large loops.
- * Forgetting that concatenating two strings requires copying their characters.
- * Assuming all programming languages handle strings in exactly the same way.
- * Using a string builder for only a few small concatenations where normal concatenation is clearer.
Follow-up questions
- Why can repeated string concatenation become O(N²)?
- How can you optimize repeated string concatenation?
- Are strings always immutable in every programming language?
- What is the time complexity of concatenating two strings?
- Why is string concatenation usually fine for small numbers of strings?