[Webkit-unassigned] [Bug 6252] JavaScript 1.6 Array.lastIndexOf

bugzilla-daemon at opendarwin.org bugzilla-daemon at opendarwin.org
Fri Aug 18 10:48:25 PDT 2006


http://bugzilla.opendarwin.org/show_bug.cgi?id=6252





------- Comment #16 from darin at apple.com  2006-08-18 10:48 PDT -------
(In reply to comment #15)
> (In reply to comment #14)
> > (From update of attachment 10095 [edit] [edit])
> > +      if ((d + length) < 0)
> > +          return jsNumber(-1);
> > +      if (d < 0)
> > +          d += length;      
> > 
> > I think it's a shame that this adds length to d twice. How about this instead:
> > 
> >     if (d < 0) {
> >         d += length;      
> >         if (d < 0)
> >             return jsNumber(-1);
> >     }
> 
> sounds good. i have a couple of questions about the above statement, however. i
> had a similar short version of the if block as you proposed before but at the
> end decided to go with my version since in case (d+length) is negative, the
> script stops before performing other tests and thus saves an extra operation.
> but then again, if it's not less than zero then it adds an expense of an extra
> addition comparison so it's kind of 50-50. what do you think? 
This is not super-important, but I'd be happy to explain my thinking.

When making a decision like this I consider what's the normal case and what's
exceptional. In the common case, d is not negative, and in that case my
suggested code does only a single branch. It's not as important to optimize the
"highly negative" case because that one usually reflects an error in a script.
It's usually best to structure things so that the normal case has as few
branches as possible.

We need to be sure this does the right thing in the NaN case as well as
negative and positive infinity -- the best way to ensure those is to make sure
we have test cases that cover those.


-- 
Configure bugmail: http://bugzilla.opendarwin.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.



More information about the webkit-unassigned mailing list