C#
String.Join
StringBuilder
performance comparison
.NET

String.Join vs. StringBuilder which is faster?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

string.Join and StringBuilder solve different string-construction problems, so the faster choice depends on the shape of the work. If you already have a collection of pieces and want one separator between them, string.Join is usually the cleanest and often the fastest option. If you are building text incrementally with loops, conditionals, and mixed types, StringBuilder is usually the better fit.

When string.Join Wins

string.Join is specialized for joining an existing sequence into one string. It knows the separator, walks the input once, and produces the final output without the repeated intermediate allocations that naive + concatenation would create.

csharp
1using System;
2
3string[] items = { "alpha", "beta", "gamma" };
4string csv = string.Join(",", items);
5
6Console.WriteLine(csv);

This is ideal for:

  • CSV-style output
  • logging a list of values
  • joining command-line arguments
  • converting collections into readable text

If the input is already a collection, string.Join is hard to beat for both clarity and performance.

When StringBuilder Wins

StringBuilder is designed for progressive construction. It becomes useful when the final number of fragments is not fixed in advance or when you need branching logic while building the result.

csharp
1using System;
2using System.Text;
3
4var builder = new StringBuilder();
5
6for (int i = 1; i <= 5; i++)
7{
8    builder.Append("Item ");
9    builder.Append(i);
10    builder.AppendLine();
11}
12
13string result = builder.ToString();
14Console.WriteLine(result);

This pattern is better for:

  • loops that append many fragments
  • multi-line report generation
  • mixed numeric and text formatting
  • conditional text assembly

Trying to force this shape into string.Join usually makes the code less natural.

A Simple Benchmark Shape

You should benchmark your own workload, but the rough pattern is consistent:

  • 'string.Join is strong for joining a ready-made list.'
  • 'StringBuilder is strong for incremental construction.'

Here is a minimal benchmark-style example:

csharp
1using System;
2using System.Diagnostics;
3using System.Linq;
4using System.Text;
5
6string[] data = Enumerable.Range(0, 10000).Select(i => i.ToString()).ToArray();
7
8var sw = Stopwatch.StartNew();
9string joined = string.Join(",", data);
10sw.Stop();
11Console.WriteLine($"Join: {sw.ElapsedMilliseconds} ms");
12
13sw.Restart();
14var sb = new StringBuilder();
15for (int i = 0; i < data.Length; i++)
16{
17    if (i > 0) sb.Append(",");
18    sb.Append(data[i]);
19}
20string built = sb.ToString();
21sw.Stop();
22Console.WriteLine($"StringBuilder: {sw.ElapsedMilliseconds} ms");

This is fine for learning, but for real decisions use BenchmarkDotNet instead of Stopwatch, because microbenchmark noise is easy to misread.

The Real Comparison to Avoid

The bad comparison is often not string.Join versus StringBuilder. It is either of those versus repeated + concatenation inside loops.

csharp
1string text = "";
2for (int i = 0; i < 10000; i++)
3{
4    text += i.ToString();
5}

That pattern creates many temporary strings and is usually the one worth replacing first.

How to Choose in Practice

Ask one question: do I already have the pieces?

  • If yes, use string.Join.
  • If no, and the text is being built step by step, use StringBuilder.

Also consider readability. A tiny performance win is not worth making text-building code harder to understand unless profiling shows the code is hot enough to matter.

Common Pitfalls

  • Using StringBuilder for a simple one-line join of an existing collection.
  • Replacing clear code with micro-optimized code before profiling.
  • Comparing string.Join to StringBuilder when the real problem is + concatenation in a loop.
  • Forgetting that StringBuilder still has overhead for very small strings.
  • Drawing big conclusions from noisy Stopwatch tests instead of proper benchmarks.

Summary

  • 'string.Join is usually best for joining an existing sequence with a separator.'
  • 'StringBuilder is usually best for incremental and conditional string construction.'
  • Both are better than repeated + concatenation in loops.
  • Choose based on workload shape first and benchmark second.
  • Prefer the clearest correct approach unless profiling proves the code is hot.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.