Re: Are JavaScript strings mutable?



VK said on 19/04/2006 6:19 AM AEST:
Water Cooler v2 wrote:

Are JavaScript strings mutable? How're they implemented -


As a sequence of Unicode-16 characters. That also means that unlike in
low-level languages in JavaScript there is not direct relation byte <>
character. Also JavaScript doesn't have Char datatype (nor Byte for
this matter), it knows only strings containing single Unicode-16
character.

This is the max one can read out of specs IMHO because the internal
implementation was left totally up to engines' producers. So are they
mutable, immutable or carried by little green gnomes :-) depends I
guess on the particular browser.

Say in JScript and JScript.Net (IE) many parts are borrowed from the
system. In the particular JScript String object is layer on
System.String and it is immutable - as System.String itself.
So in the proposed case:
txt += "foo bar";
the engine creates anonymous string object for "foo bar", creates new
joined string, set the reference to this new string from txt and marks
both former txt and "foo bar" as GC ready.

if txt has not already been given a string value (say empty string "" or some other value), the result will be:

"undfinedfoo bar"

If you conclude from this that the string concatenations are relatively
slow in JScript - you are hundred times right :-)

You need to define relative to what. JavaScript is much slower than compiled languages, but it isn't built for speed. Consider:


Method 1: txt += 'more text';

Method 2: txt = txt + 'more text';

Method 3: txt = [txt, 'more text'].join('');


In Firefox, method 1 is fastest but all 3 methods take about the same time for say less than 10,000 concatenations.

In IE, method 3 is about as fast as Firefox method 3, but 1 takes 20 times longer than 3 and method 2 about 6 times longer than that. A test case is provided below (careful, IE takes over minute to run it, Firefox a couple of seconds).


<script type="text/javascript">

var ipsum = ['Facilisis ', 'illum ', 'et ', 'qui ', 'wisi ',
'nonummy ', 'sit, ', 'dolore ', 'delenit ', 'in ', 'ad ', 'at, ',
'vel ', 'wisi. ', 'Ut ', 'dolor ', 'nisl ', 'laoreet ', 'odio, ',
'delenit. ', 'Facilisi ', 'esse ', 'elit ', 'eu ', 'vel '];

function getRand(r){
return (Math.random()*r)|0;
}

var iterations = 30000;
var catString = '';
var catArray = [];
var j = ipsum.length;

var s = new Date();
var i = iterations;
while (i--){
catString += ipsum[getRand(j)];
}
var f = new Date();
var txt = 'Using += ' + (f-s);

s = new Date();
i = iterations;
while (i--){
catString = catString + ipsum[getRand(j)];
}
f = new Date();
txt += '<br>Using = + ' + (f-s);

s = new Date();
i = iterations;
while (i--){
catArray.push(ipsum[getRand(j)]);
}
var x = catArray.join('');
f = new Date();
txt += '<br>Using push/join ' + (f-s);

document.write(txt);

</script>


Incidentally, there is an impsum lorem generator here:

<URL:http://www.lindquist.dk/tools/LorumIpsumGenerator.asp>



--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
.



Relevant Pages

  • Re: Trying to limit input ot certain numbers
    ... " part is and were in the help files I can learn about it. ... If you want one character from a string, then use the array-access notation to extract just that one character: ...
    (alt.comp.lang.borland-delphi)
  • Re: Updating string/array items
    ... I'm not entirely familiar with the norms and standard libraries of ... JavaScript so if the answer to this is yesterday's news, ... string by accessing that character with an index, ...
    (comp.lang.javascript)
  • Updating string/array items
    ... I'm not entirely familiar with the norms and standard libraries of ... JavaScript so if the answer to this is yesterday's news, ... string by accessing that character with an index, ...
    (comp.lang.javascript)
  • Re: Dynamically switch Javascript
    ... to load the contents of the executed Javascript into my div tag. ... So the loop does not even execute. ... A call gets made to a server, and it returns a string. ...
    (comp.lang.javascript)
  • Re: [PHP] A two flavored post
    ... I asked this question on the javascript list, ... var someLinkHref = document.getElementById.href; ... what I needed to be in the href string. ...
    (php.general)