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 2014/01/03 02:26:11 UTC

try/catch vs in

Hi,

While looking at some data grid code I noticed this:

            try
            {
                data = data[dataField];
            }
            catch(e:Error)
            {
                data = null;
            }

And I was curious to why try catch is used rather than this:

            if (dataField in data)
            {
                data = data[dataField];
            }
            else
            {
                data = null;
            }

I know "in" can be expensive but so is try catch. I've run each though scout and it looks like the in operator is orders of magnitude faster and caused no garbage collection both when the field exists and when it doesn't. Anyone have any insight/something to add?

Thanks,
Justin

Re: try/catch vs in

Posted by Alex Harui <ah...@adobe.com>.
Many moons ago we were told that "in" was slow but it wouldn't surprise me
if it got better.  If Scout says it is better and other tests indicate the
same, then it seems like a good change.

-Alex

On 1/2/14 7:53 PM, "Justin Mclean" <ju...@classsoftware.com> wrote:

>Hi,
>
>> Any chance data can be null at that point? Does your code handle that
>>well?
>
>Already handled in the ADG case but I check the other I've just changed.
>
>       if (typeof(data) == "object" || typeof(data) == "xml")
>       {
>            if (dataField != null)
>		if (dataField in data)
>            		data = data[dataField];
>	    	else
>			data = null;
>       }
>
>Thanks,
>Justin


Re: try/catch vs in

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

> Any chance data can be null at that point? Does your code handle that well?

Already handled in the ADG case but I check the other I've just changed.

       if (typeof(data) == "object" || typeof(data) == "xml")
       {
            if (dataField != null)
		if (dataField in data)
            		data = data[dataField];
	    	else
			data = null;
       }

Thanks,
Justin

RE: try/catch vs in

Posted by "Michael A. Labriola" <la...@digitalprimates.net>.
>I know "in" can be expensive but so is try catch. I've run each though scout and it looks like the in operator is orders of magnitude faster and caused no garbage collection both when the field exists and when it >doesn't. Anyone have any insight/something to add?


Any chance data can be null at that point? Does your code handle that well? If you are good on those fronts I think it's a very positive change. I hate when try/catch is used in this way.

Mike


Re: try/catch vs in

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

With that change it looks like the ADG tests run a few seconds faster but the running time is a little variable.

Justin