For a specific application where you know the value of classes you  
can get away with using a regex, but because it's a fragile  
implementation, it might still be wise to avoid. (A simple == is  
better.)  The reason it's fragile, is the regexp would also match the  
valid class name "myclass2".  You can't just wrap the text string in  
"\b" looking for word boundaries, because '-' is valid in class  
names, but triggers a "word boundary".

I typed out a handful of other possible optimizations for  
hasClassName based on my recollection of how it was implemented.   
Then I checked the source, and found these optimizations were all  
implemented sometime around the tail end of last year (the old code,  
which is what I remembered, was horribly inefficient). Make sure  
you're using a recent Prototype version.  The core team put a lot of  
work into speedup of $$, et al.

... One possible speedup of the current hasClassName code (which _is_  
based on a regex), would be to make the groups non-capturing: several  
of the js references I have indicate the regex is faster that way.   
It may be worth testing.


TAG

On May 28, 2007, at 10:42 AM, [EMAIL PROTECTED] wrote:

>
> Aha, if I change my custom function to use Element.hasClassName(elt,
> 'myclass') instead of elt.className=='myclass' , then it jumps from
> 16ms to ~250ms.  This matches $$('div.blah'), which also took
> ~250ms.
>
> So, FWIW, if you don't need Element.hasClassName -- that is, you're
> only dealing with a single class -- it looks like you can score an
> order of magnitude speed improvement by using "if
> elt.className=='myclass'" instead.
>
> Whoa!  Hold on... even elt.className.match('myclass') is way faster
> (16ms) than hasClassName.  And that takes care of the multiple-
> classname issue, right?
>
> Has anyone done any detailed profiling of hasClassName?  Looks like
> improvements in that might speed up IE's $$ significantly.
>
> Altay


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to