Algorithms and Data Structures · Junior · Question 45 of 75
What is the complexity of building a string with += in a loop in modern JavaScript engines?
- ✓Correct answer: Usually fine: engines optimize repeated concatenation
- 2Always O(n³)
- 3It throws once the string is longer than 1,000 characters
Explanation
Strings are immutable, but engines such as V8 use ropes, so repeated += is not copied every time and stays practical. In languages without this optimization the classic advice is to collect parts in an array and join them.