For example, suppose I have a long string primitive called str
. I know I can use str.substr
because the browser, behind the scenes,
- Converts
str
into a string wrapper object, equivalent to usingnew String(str)
- Calls the
substr
method with the correct parameters on theString
object returned by step 1 - Disposes of the
String
object - Returns the string (primitive) from step 2.
(https://stackoverflow.com/a/9110389/1529630)
But what happens if I want to use str.substr
lots of times? For example
var positions = [1, 5, 8, 9, 15, ...], //long array
substrings = new Array(positions.length);
for ( var i = positions.length - 1; i >= 0; --i ) {
substrings[i] = str.substr(positions[i], 2);
}
Should I convert str
to an object before that loop in order to avoid creating and destroying an object at every step?