Skip to main content

Bug Tracker

Side navigation

#783 closed enhancement (wontfix)

Opened January 11, 2007 03:21PM UTC

Closed March 24, 2007 03:20AM UTC

Flip/Unroll loops to improve speed (up to 67% faster than for loop)

Reported by: jamie{dot}mason{at}b Owned by:
Priority: minor Milestone:
Component: core Version:
Keywords: optimisation speed loops Cc:
Blocked by: Blocking:
Description

It's easiest to link to this article which explains the differences in full;

http://www.peachpit.com/articles/article.asp?p=31567&seqNum=6&rl=1

To summarise, jQuery could be faster if the for loops were replaced with do..while loops (at no expense of file size), or even faster using duff's device (at the expense of file size).

There are a number of different approaches described in the article.

I'm happy to implement these changes against 1.1b if it was agreed they'd be of benefit? And try to do some other refactoring if possible.

Thanks,

Jamie Mason

  • Senior Web Designer, BSkyB
Attachments (0)
Change History (4)

Changed January 11, 2007 04:49PM UTC by anonymous comment:1

That looks sweet. It would be nice if you guys could make jquery even faster. :)

Changed January 22, 2007 11:42AM UTC by anonymous comment:2

i'm very sceptic about micro-optimizations like these.

especially when code readability (and maintainability) suffers.

*if* you think it's worth the trouble then i'd suggest to implement it as a generic preprocessor

(like the packer) but leave the original code untouched.

generally i'd say: don't bother.

these things will be optimized as browser implementations mature and who's supposed to test all

your little micro-optimizations against all available browsers anyways?

what if your handcrafted reversed loop performs 40% better in firefox but 40% worse in IE?

what if your carefully designed duff device performs 80% better in firefox 2.0 but 80% worse in firefox 2.5?

i think there are many things to be done to make jquery better. micro-optimizations is not one of them.

Changed January 23, 2007 02:49PM UTC by jamie{dot}ma comment:3

Readability;

  • I agree that Duff's device is less readable but the do...while is no less readable but far quicker so could be acceptable.

"who's supposed to test all your little micro-optimizations against all available browsers anyways?"

  • If it was decided this should be done, I would be happy to do the benchmarking.

"what if your handcrafted reversed loop performs 40% better in firefox but 40% worse in IE? what if your carefully designed duff device performs 80% better in firefox 2.0 but 80% worse in firefox 2.5?"

  • It's standard practice to run tests and make a per-case decision based on the results. In your examples you may decide to run alternative code based on those conditions, or decide a for loop will suffice.

"i think there are many things to be done to make jquery better. micro-optimizations is not one of them."

  • You may be right, but from the tone and substance of your comment (and the assumption you're not John Rezig!) I (respectfully) can't help but feel you've not done anything like this before. My opinion is that it's all about running benchmarks on the code, making changes, re-testing them and making a decision on the best implementation.

Changed March 24, 2007 03:20AM UTC by john comment:4

resolution: → wontfix
status: newclosed

@Jamie - For now, we're going to pass on duff's device; we currently have a good balance of speed and filesize that we like. As for do .. while - I'll take it into consideration when dealing with the selector speed-ups. (We do have some loops that might benefit from some additional speed.) Although, the vast majority of the slowdown in jQuery is related to DOM-related operations - and there's not much way of getting around those.

In the future, feel free to post any suggestions or recommendations that you have to the dev mailing list - we tend to discuss things of this nature, there.