You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@flex.apache.org by Justin Mclean <ju...@classsoftware.com> on 2017/06/06 09:13:01 UTC

Re: [2/4] git commit: [flex-asjs] [refs/heads/release0.8.0] - looks like we get '' (empty string) sometimes which coerces to 0. We want it to be NaN

Hi,

This is probably a simpler / less costly way of doing the same thing:

var pixels:Number = NaN;
var strpixels:String = element.style.width as String;
if (strpixels.indexOf('%') === -1)
    pixels = parseFloat(strpixels);

Justin


Re: [2/4] git commit: [flex-asjs] [refs/heads/release0.8.0] - looks like we get '' (empty string) sometimes which coerces to 0. We want it to be NaN

Posted by Harbs <ha...@gmail.com>.
It sounds like a micro-optimization, but it saves some bytes and doesn’t look like a functional change, so go knock yourself out… ;-)

> On Jun 6, 2017, at 1:43 PM, Justin Mclean <ju...@classsoftware.com> wrote:
> 
> Hi,
> 
> Just profiled these with a variety of inputs and the first is around 3 times as fast as the second.
> 
> I added a null check to stop the RTE, if the null check is not needed it can be removed from both.
> 
> public function test(str:String):Number {
>    var pixels:Number = NaN;
>    if (str !== null && str.indexOf('%') === -1)
>        pixels = parseFloat(str);
> 
>    return pixels;  
> }
> 
> public function test2(str:String):Number {
>    var pixels:Number;
>    if (str !== null && str.indexOf('%') != -1)
>        pixels = NaN;
>    else if (str !== null && str.length == 0)
>        pixels = NaN;
>    else
>        pixels = parseFloat(str);
> 
>    return pixels;
> }
> 
> Thanks,
> Justin
> 


Re: [2/4] git commit: [flex-asjs] [refs/heads/release0.8.0] - looks like we get '' (empty string) sometimes which coerces to 0. We want it to be NaN

Posted by Justin Mclean <ju...@classsoftware.com>.
Hi,

Just profiled these with a variety of inputs and the first is around 3 times as fast as the second.

I added a null check to stop the RTE, if the null check is not needed it can be removed from both.

public function test(str:String):Number {
    var pixels:Number = NaN;
    if (str !== null && str.indexOf('%') === -1)
        pixels = parseFloat(str);

    return pixels;  
}

public function test2(str:String):Number {
    var pixels:Number;
    if (str !== null && str.indexOf('%') != -1)
        pixels = NaN;
    else if (str !== null && str.length == 0)
        pixels = NaN;
    else
        pixels = parseFloat(str);

    return pixels;
}

Thanks,
Justin


Re: [2/4] git commit: [flex-asjs] [refs/heads/release0.8.0] - looks like we get '' (empty string) sometimes which coerces to 0. We want it to be NaN

Posted by piotrz <pi...@gmail.com>.
I think it would be great! Let see what Alex thing. 

Piotr



-----
Apache Flex PMC
piotrzarzycki21@gmail.com
--
View this message in context: http://apache-flex-development.2333347.n4.nabble.com/Re-2-4-git-commit-flex-asjs-refs-heads-release0-8-0-looks-like-we-get-empty-string-sometimes-which-cN-tp62159p62168.html
Sent from the Apache Flex Development mailing list archive at Nabble.com.

Re: [2/4] git commit: [flex-asjs] [refs/heads/release0.8.0] - looks like we get '' (empty string) sometimes which coerces to 0. We want it to be NaN

Posted by Justin Mclean <ju...@classsoftware.com>.
Hi,

> var strpixels:String = element.style.width as String; 
> if (strpixels.indexOf('%') === -1) 
> 
> We may have still RTE ?

Yep it would also RTE if strpixels was null.

So perhaps this instead?
> if (strpixels !== null && strpixels.indexOf('%') === -1) 


Justin

Re: [2/4] git commit: [flex-asjs] [refs/heads/release0.8.0] - looks like we get '' (empty string) sometimes which coerces to 0. We want it to be NaN

Posted by piotrz <pi...@gmail.com>.
I think it would be good to change all those with your proposition. For me
it's cleaner, but with this one from you:

var strpixels:String = element.style.width as String; 
if (strpixels.indexOf('%') === -1) 

We may have still RTE ?

Piotr



-----
Apache Flex PMC
piotrzarzycki21@gmail.com
--
View this message in context: http://apache-flex-development.2333347.n4.nabble.com/Re-2-4-git-commit-flex-asjs-refs-heads-release0-8-0-looks-like-we-get-empty-string-sometimes-which-cN-tp62159p62166.html
Sent from the Apache Flex Development mailing list archive at Nabble.com.

Re: [2/4] git commit: [flex-asjs] [refs/heads/release0.8.0] - looks like we get '' (empty string) sometimes which coerces to 0. We want it to be NaN

Posted by Justin Mclean <ju...@classsoftware.com>.
Hi,

> You propose changing whole this part [1] with yours ? 

Or something similar / along those line that was off the top of my head and had not been tested.

Perhaps if null is never passed in then it doesn’t need to be checked for and the code may not RTE?

Thanks,
Justin

Re: [2/4] git commit: [flex-asjs] [refs/heads/release0.8.0] - looks like we get '' (empty string) sometimes which coerces to 0. We want it to be NaN

Posted by piotrz <pi...@gmail.com>.
Justin,

You propose changing whole this part [1] with yours ? 

[1] https://paste.apache.org/L1Mq

Just wanted to understand.

Thanks,
Piotr



-----
Apache Flex PMC
piotrzarzycki21@gmail.com
--
View this message in context: http://apache-flex-development.2333347.n4.nabble.com/Re-2-4-git-commit-flex-asjs-refs-heads-release0-8-0-looks-like-we-get-empty-string-sometimes-which-cN-tp62159p62164.html
Sent from the Apache Flex Development mailing list archive at Nabble.com.