I agree with Mike about the importance of readable code. The jQuery  
core used to have a lot more of this sort of trick than it does now. I  
like it and understand it much more now.

If I recall correctly, indexOf() is quite a bit faster than test().
Also, this bit from Crockford's JavaScript: the Good Parts suggests  
that sticking with indexOf is probably best:
"...the bitwise operators convert their number operands into integers,  
do their business, and then convert them back. In most languages,  
these operators are very close to the hardware and very fast. In  
JavaScript, they are very far from the hardware and very slow."

--Karl

____________
Karl Swedberg
www.englishrules.com
www.learningjquery.com




On Nov 30, 2009, at 6:33 PM, Michael Geary wrote:

> I don't care much for the single var with multiple variables at the  
> top of a function. I like seeing the "var" on a variable where it is  
> first used. It makes it easier for me to see that there actually  
> *is* a "var". But you're right, it does save a few bytes. Maybe not  
> much after the code is gzipped, though.
>
> That tilde trick is interesting, but if you're trying to save code  
> bytes you could also use:
>
> if( /^\./.test(str) )
>
> which is even shorter. Maybe indexOf is faster though.
>
> The problem with the tilde is it means you have to work at  
> understanding what the code does. I had to write out a truth table  
> with ~-1, ~0, and ~1 to make sure I understood it.
>
> I just don't see much value in writing tricky code if it makes it  
> harder to understand. You may as well go for single character  
> variable names while you're at it! Instead I prefer to see more  
> readable code, and let the minifying and gzipping take care of  
> compressing it.
>
> OTOH, I do very much like to use !! when I need to convert an  
> arbitrary truthy/falsy value to a boolean true or false. So go  
> figure. :-)
>
> -Mike
>
> On Mon, Nov 30, 2009 at 2:21 PM, lrbabe <lrb...@gmail.com> wrote:
> I'm wondering if there are some arguments against following this rule.
> There are many places in the code where it is ignored.
>
> I've also seen a trick to save another few bites where indexOf is
> involved by using the ~ operator:
> replacing tests such as
> if ( str.indexOf(".") >= 0 )
> by
> if ( !~str.indexOf(".") )
>
> and tests such as
> if ( str.indexOf(".") === -1 )
> by
> if ( ~str.indexOf(".") )
>
> Any contraindication for the 'single var rule' and the 'tilde trick'?
> Any other way to reduce a javascript file size?
>
> Thank you in advance,
>
> -- Louis-RĂ©mi
>
> --
>
> You received this message because you are subscribed to the Google  
> Groups "jQuery Development" group.
> To post to this group, send email to jquery-...@googlegroups.com.
> To unsubscribe from this group, send email to 
> jquery-dev+unsubscr...@googlegroups.com 
> .
> For more options, visit this group at 
> http://groups.google.com/group/jquery-dev?hl=en 
> .
>
>
>
>
> --
>
> You received this message because you are subscribed to the Google  
> Groups "jQuery Development" group.
> To post to this group, send email to jquery-...@googlegroups.com.
> To unsubscribe from this group, send email to 
> jquery-dev+unsubscr...@googlegroups.com 
> .
> For more options, visit this group at 
> http://groups.google.com/group/jquery-dev?hl=en 
> .

--

You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-...@googlegroups.com.
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en.


Reply via email to