Concatenating Large Number Of String the Right Way #DartTip

PAUL
1 min readJan 2, 2022

--

Concatenation of Strings can be done in a variety of ways in Dart. But to glue large number of strings together needs more extra care when you’re looking at performance pertaining saving memory and less execution time.

string1 + string2. This is the most straightforward. This the easiest when it comes to smaller and readability apart from String interpolation.

However, each time you call “+” there is a performance hit and when you concatenate two strings you create a new object which needs to be garbage collected. This create lot of temporary objects, which is inefficient. (Also note, calling the + on null value throws an exception).

Here is an example:

Using the StringBuffer class in the other hand can gather the components quite efficiently and convert them to a string only when needed.

Here is an example:

Happy Coding New Year!!!

--

--

PAUL
PAUL

Written by PAUL

Mobile Engineer | Flutter | TechTalks | Guitar

Responses (2)