I noticed a couple of things:
1. There’s lots of String(val) casts in FlexJS code. This practice is 
considered “bad” practice in Javascript where implicit conversion is generally 
quicker. So in a case where a number can be converted implicitly, the cast 
should be completely skipped and even when a number needs to be converted to a 
string, “” + val is more efficient than String(val). This is especially true in 
FlexJS where (I believe) such code will result in 
org.apache.flex.Language.as(val,”String”).

2. String concatenation: I’m not sure how much of an issue this is in current 
browsers, but using aray.join() to build a string was MUCH faster than building 
strings using the plus operator. For longer strings the difference was dramatic.

I just did a quick test in Chrome using the following functions:
function timePlusEquals(){
        var start = new Date();
        var i=-1;
        var len  = 100000;
        var str = "";
        while(++i<len)
        {
                str+= "a";
        }
        var end = new Date();
        console.log(end.getTime()-start.getTime());
}
function timePlus(){
        var start = new Date();
        var i=-1;
        var len  = 100000;
        var str = "";
        while(++i<len)
        {
                str= str+"a";
        }
        var end = new Date();
        console.log(end.getTime()-start.getTime());
}
function timeJoin(){
        var start = new Date();
        var i=-1;
        var len  = 100000;
        var arr = [];
        while(++i<len)
        {
                arr.push("a");
        }
        var str = arr.join("");
        var end = new Date();
        console.log(end.getTime()-start.getTime());
}

and these were my results:
timePlusEquals()
20
timePlus()
17
timeJoin()
7

Interestingly enough, increasing the string size to “abced” had very little 
effect on performance. Also interesting was that running the functions multiple 
times sequentially knocked the time down to between 1 and 3 ms for all 
functions. It looks like Chrome has some pretty good optimizations in place.

In Firefox it was interesting to note that “plusEquals was the fastest in all 
my tests. It took about 1! ms to complete. join() was the slowest at 3 ms. 
Increasing the number of iterations to 10000000 brought join() into the lead:
timePlusEquals()
440
timePlus()
413
timeJoin()
238

But Chrome was reversed (and again slower than Firefox).

I think the takeaway (at least for Chrome and Firefox) is that this is not the 
issue I thought it was.

However, string casting does seem to be an issue (at least a small one):

function stringCast1(){
        var start = new Date();
        var i=-1;
        var len  = 10000000;
        var str = "";
        while(++i<len)
        {
                str= String(188);
        }
        var end = new Date();
        console.log(end.getTime()-start.getTime());
};
function stringCast2(){
        var start = new Date();
        var i=-1;
        var len  = 10000000;
        var str = "";
        while(++i<len)
        {
                str= ""+188;
        }
        var end = new Date();
        console.log(end.getTime()-start.getTime());
};

In Firefox:
stringCast1()
3874
stringCast2()
4

That’s pretty dramatic.

In Chrome, less so; but still:
stringCast1()
63
stringCast2()
11

Reply via email to