Which solution has better performance: StringBuilder or String Interpolation-Concatenation

I am writing files to disk using Scala.

To create the whole String that will be written to the file, I am currently iterating over my data and appending all the information to a StringBuilder object.

for instance:

val moreData = getMoreData
strBuilder.append("even more data")

At the end of the operation I then call the StringBuilder's toString method, and write to the Path.

I know Scala has compilation optimizations for Strings, so my questions are:

Which approach has the better performance. String-Interpolation-Concatenation or StringBuilder?

Are these compilation optimizations somehow related to StringBuilder? In other words, are there optimizations for StringBuilder append operation?


  • String interpolation concatenation uses a StringBuilder to generate its results. It would be possible to optimize string interpolation more, but as it stands it is mostly designed for expressive power, not performance. You should use StringBuilder if you know that string creation is going to be limiting, and it's not too hard to do so. If you don't know, or you know that it isn't a major issue, string interpolation is usually much easier to read, so you should prefer it in most instances.