In Mathematics, the existence of a solution is often sufficient.
In Computers, this is not always the case. Timing is often an unexpected problem. See the problems here? See below for the answer.
Javascript
PHP
Python
Java
C
Doing ten times as many string concatenations can cause an exponential increase in time.
Both PHP and Java suffer huge delays, but Javascript and Python only have linear delays.
Fundamentally, there are two ways to store variable length strings.
You can either (1) store the length of the string and each character, or (2) you can use a special character (as long as it is not part of any string!).
String
1. Store Length
2. Special Marker
""
0
@
"cat"
3 c a t
c a t @
"elephant"
8 e l e p h a n t
e l e p h a n t @
This is, of course, greatly simplified and there many ways to avoid the problem.
In the first case, we cannot have extremely long strings.
If only reserve two bytes (unsigned) for the string length, then the longest string allowed will be 216-1 (65,535) characters.
In the second case, we have to search the string to find the marker (@ in our case, typically an ASCII NUL or zero is used).
This can be very slow if you have to keep finding the end of the string. And that is why PHP and Java are so slow for long strings.
All that time is spent searching for the end of the string on each operation, which is very inefficient, but not at all obvious.