K
Kamjah Kogumah
A Dev Opera article [1] suggest using code 2 instead of code 1 in
performance-critical functions.
code 1:
a += 'x' + 'y';
code 2:
a += 'x';
a += 'y';
But I thought string concatenation takes longer time when involved
strings are long.
code 1 does two concatenations : 'x'+'y' and a+'xy'
code 2 does : a+'x' and a+'y'.
If a is long enough, code 2 should take twice much time as code 1
because it takes small time to do 'x'+'y' and it takes long to do
a+something. Am I missing something?
[1] Dev Opera - Efficient JavaScript - Use strings accumulator-style
http://dev.opera.com/articles/view/efficient-javascript/?page=2#stringaccumulator
performance-critical functions.
code 1:
a += 'x' + 'y';
code 2:
a += 'x';
a += 'y';
But I thought string concatenation takes longer time when involved
strings are long.
code 1 does two concatenations : 'x'+'y' and a+'xy'
code 2 does : a+'x' and a+'y'.
If a is long enough, code 2 should take twice much time as code 1
because it takes small time to do 'x'+'y' and it takes long to do
a+something. Am I missing something?
[1] Dev Opera - Efficient JavaScript - Use strings accumulator-style
http://dev.opera.com/articles/view/efficient-javascript/?page=2#stringaccumulator