Everyone uses StringBuilder for speed…
but almost nobody knows WHY it becomes slow after heavy use.
⚠ Problem
When StringBuilder grows beyond its internal buffer → it doubles memory and copies everything.
Large loops = huge perf waste.
✔ Life-Saving Fix
var sb = new StringBuilder(50000); // pre-allocate
If you know approximate size → pre-allocating saves massive CPU.
💡 Bonus
Don’t ever do:
sb.ToString(); sb.Append(...); // ❌ forces buffer copy twice
Call ToString() only at the end.
