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
On May 28, 12:24 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> Hi TAG-
>
> Thanks for the reply.
>
> Here what's up. I've got an arbitrary number of divs, with
> class='qedit', and id='{uniqueid}_qedit'. The user can click on an
> "edit" link to open up a qedit div for editing, but s/he can only edit
> one at a time. In other words, when you click 'edit,' it should find
> and close any currently-open divs, and open the div you clicked on.
>
> Hm, now that I actually write out my requirements, I suppose I could
> simply store the currently-open id in a global, which would be a
> little more straightforward. Doh. It's time to refactor.
>
> But, for the sake of discussion -- and cause I've run into this issue
> in other situations -- here's my original implementation:
>
> function toggle_qedit( prefix ) {
> var t0 = new Date();
>
> var all_qedits = $$('.qedit');
> //var all_qedits = document.getElementsByClassName('qedit');
> //var all_qedits = $$('div.qedit');
> //var all_qedits = find_elts_by_class( 'div', 'qedit' );
>
> var t1 = new Date();
> $('debug').innerHTML = "It took " + (t1.getTime()-t0.getTime()) +
> "ms to do that.";
>
> all_qedits.each( function(q) {
> var qid = q.id.split('_')[0];
> if ( qid == prefix && q.style.display == 'none' ) {
> div_toggle( qid, true );
> }
> else {
> div_toggle( qid, false );
> }
> });
>
> }
>
> It loops through all the divs with class="qedit", and hides each of
> them unless it matches the div you clicked.
>
> There are 15 of these divs on my test page.
>
> For optimization, you can see that I tried four ways to get the
> elements:
> - $$('.qedit')
> - document.getElementsByClassName('qedit')
> - $$('div.qedit')
> - find_elts_by_class( 'div', 'qedit') --> my own custom function,
> which is not DOM-specific*
>
> Anything else I should try?
>
> Here's the breakdown:
>
> 4172ms -- $$('.qedit')
> 3797ms -- document.getElementsByClassName
> 266ms -- $$('div.qedit')
> 16ms -- my custom function
>
> So clearly, looking for div.qedit instead of just .qedit helped a lot,
> but it was still trumped by my non-prototype solution. I don't get
> it. Do you see any glaring errors in my implementation of $$?
>
> Altay
>
> * FYI, here's my custom function:
>
> function find_elts_by_class( tagname, classname ) {
> var target_elts = [];
> if (Prototype.BrowserFeatures.XPath ) {
> target_elts = $$( tagname + '.' + classname );
> }
> else {
> var elts = document.getElementsByTagName( tagname );
> for (var i=0;i<elts.length;i++) {
> if (elts[i].className == classname) {
> target_elts.push( elts[i] );
> }
> }
> }
> return target_elts;
>
> }
>
> On May 27, 11:54 pm, Tom Gregory <[EMAIL PROTECTED]> wrote:
>
>
>
> > What steps have you taken to optimize your query? Have you
> > experimented with different ways to return the same result?
>
> > Show us your query, and describe your page.
>
> > TAG
>
> > On May 27, 2007, at 9:05 PM, [EMAIL PROTECTED] wrote:
>
> > > While $$ and getElementsByClassName are ridiculously speedy in Firefox
> > > (thanks to XPath), I'm finding both functions to be intolerably slow
> > > in IE. The page I'm working with has fewer than 1000 elements --
> > > doesn't seem THAT big -- and it's still taking upwards of 4 seconds
> > > (!) to pick out the right ones.
>
> > > Are there documented problems with particular versions/builds of IE?
> > > (Perhaps I'm just unlucky enough to be testing on one of those
> > > builds.)
>
> > > Those functions are so damn convenient in theory, but are causing me a
> > > lot of headaches in practice. How do you all deal with this issue?
> > > Am I missing something obvious here?
>
> > > Thanks,
> > > Altay- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---