Incrementally concatenating strings means a lot of intermediate
strings get allocated only to be thrown away. The result is O(n**2)
in both space and time.
See this post about Shlemiel the painter's algorithm for a good
description about the quadratic behavior involved.
Storing each fragment in an array and joining them all together
in the end at least avoids allocating ever-longer intermediate
strings. I have not run any benchmarks on very long strings,
though. That might still be interesting to do.
Coverage remained the same at 80.769% when pulling 09f9b0ea0268ec805c94f36106b38222c957d114 on masak:master into 48e1f4587169d54a57f4bb4693d8ee1ea5b012f5 on mathiasbynens:master.
Coverage remained the same at 80.769% when pulling 09f9b0ea0268ec805c94f36106b38222c957d114 on masak:master into 48e1f4587169d54a57f4bb4693d8ee1ea5b012f5 on mathiasbynens:master.
Coverage remained the same at 80.769% when pulling 09f9b0ea0268ec805c94f36106b38222c957d114 on masak:master into 48e1f4587169d54a57f4bb4693d8ee1ea5b012f5 on mathiasbynens:master.
Incrementally concatenating strings means a lot of intermediate strings get allocated only to be thrown away. The result is
O(n**2)
in both space and time.See this post about Shlemiel the painter's algorithm for a good description about the quadratic behavior involved.
https://www.joelonsoftware.com/2001/12/11/back-to-basics/
Storing each fragment in an array and joining them all together in the end at least avoids allocating ever-longer intermediate strings. I have not run any benchmarks on very long strings, though. That might still be interesting to do.