Wednesday, June 28th, 2006

Speeding up Prototype’s $$ Selector

Category: Programming, Prototype

<>p>Sylvain Zimmer has written a performance upgrade for Prototype’s $$ function that he claims is up to twenty times faster.

Sylvain’s ideas behind the patch are as follows:

  • Forwarding the call to the old $$ if the selector expression is too complicated (currently : if it uses attributes)
  • Replacing regular expressions with a simple parser
  • Minimizing the number of operations done on each iteration.
  • Trying to use getDocumentById() instead of getDocumentByTagName() when possible.
  • Avoiding recursive functions with return values.
  • Not being afraid of some “old-style” code if it speeds up the execution ;-)

Now, lets see if Sam applies it…

Posted by Rob Sanheim at 9:35 am
8 Comments

+++--
3.7 rating from 33 votes

8 Comments »

Comments feed TrackBack URI

Thanks a lot for the mention!

Remember, my patch is open for discussion, I’d appreciate any feedback that can improve it ;-)

(BTW, I think there is a typo in the title? SpeeDing ;p)

Thanks again!

Comment by Sylvain — June 28, 2006

Heh, yep fixed the typo.

Comment by Rob Sanheim — June 28, 2006

NICE! I need this bad!

Comment by Liming — June 28, 2006

[...] I was ecstatic when I found (via Ajaxian) that Sylvain Zimmer has sped up the $$ function quite a bit. I implemented it in the test area of MasterWish and have been very pleased with the results! Here’s what Sylvain has to say on his mod: Prototype’s current code is quite elegant (as always!) but very slow, so I wrote an add-on that makes this function up to 20 times faster in most usual cases (tested on IE6, Firefox 1.5 and Opera). [...]

Pingback by BorkWeb » Speeding Up Prototype’s $$ Selector — June 29, 2006

Liming,
Your bottleneck when doing web programmingis generally not disk access — it is the network. For example, accessing my router is a 2 ms roundtrip. My hard drive seek-time is measured in nanoseconds. A profiler would reveal that you most likely do not need this.

Comment by Hasan — June 29, 2006

Hasan: I’m no expert but I don’t think this optimization involves disk seeks as the page is most often in the ram. I think it’s more a CPU-related thing, but I could be wrong.

cheers

Comment by sylvain — June 29, 2006

[...] Ajaxian Prototype The Prototype Window Class allows you to add in-browser windows with the flick of a few … Terry Laurenzo found that he liked “the simplicity of Prototype, … [...]

Pingback by Prototype » Carbon Finance at the World Bank: Prototype Carbon Fund — July 5, 2006

@Hasan:

Yeah, imagine… someone is actually trying to optimize performance. Who would be that foolish? I mean, when I pull down a ton of data via ajax, THAT is the only thing I worry about… who cares about processing it as fast as possible once it hits the browser.

Sheesh…

Comment by Bob Obringer — July 9, 2006

Leave a comment

You must be logged in to post a comment.