On Jul 26, 2016, at 8:15 PM, Alex Harui <[email protected]> wrote:
>
>
> On 7/26/16, 1:40 AM, "Harbs" <[email protected]> wrote:
>
>> 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”).
>
> I'm not seeing this. What source code is generating String(val) calls?
> Regarding optimization, do we know if GCC will do this ("" + val)
> optimization for us?
>
> In general, there is a big TODO around type conversions.
Good question. I just checked and there’s both String() and toString() calls in
the optimized code. One example:
return RA+String(a)+bc+String(this.alpha)}; It’s coming from one of the Graphic
classes.
I added a test to toString() performance (toString() is used quite often in the
code — much more than String(), and the performance was still worse than
implicit conversions:
function stringCast1(){
var num = 188;
var start = new Date();
var i=-1;
var len = 10000000;
var str = "";
while(++i<len)
{
str= String(num);
}
var end = new Date();
console.log(end.getTime()-start.getTime());
};
function stringCast2(){
var num = 188;
var start = new Date();
var i=-1;
var len = 10000000;
var str = "";
while(++i<len)
{
str= ""+num;
}
var end = new Date();
console.log(end.getTime()-start.getTime());
};
function stringCast3(){
var num = 188;
var start = new Date();
var i=-1;
var len = 10000000;
var str = "";
while(++i<len)
{
str= num.toString();
}
var end = new Date();
console.log(end.getTime()-start.getTime());
};
Firefox:
stringCast1()
4521
stringCast2()
4
stringCast3()
200
Chrome:
stringCast1()
93
stringCast2()
25
stringCast3()
101
I think the takeaway to to avoid String() and toString() calls. I found 93
places in the minimized code where String() is being used and 548 places where
toString() is being used.
>
>>
>> 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.
>
> If I understood this, it would be hard for the compiler to generate the
> right code for all browsers. You could create a StringBuilder class that
> checks platforms and runs different code at runtime.
>
> FWIW, Flex UIDUtils uses ByteArray because, IIRC, while array.join() might
> be fast, the array.push() is horribly slow and generates a lot of GC
> activity.
Apparently this is not much of an issue in modern browsers, so I think our
efforts are better invested elsewhere.