Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I've made a selector library at https://github.com/minitech/Sprint.js and it appears to be much slower than jQuery on the ID selectors #test and #test2, and on the selector *. Can anyone point out some optimizations that could be made to make it faster?

share|improve this question
1  
My first question would by "why"? Why implement your own selector library? Sizzle is freely available, widely vetted and tuned and is what is used inside of jQuery. JQuery/Sizzle optimizes selectors like #test to just call document.getElementById(). Are you doing that? –  jfriend00 Nov 1 '11 at 0:18
    
You do a bunch of croft for sprint("#id") and you don't cache the live nodelist returned from "*" –  Raynos Nov 1 '11 at 0:30
    
@jfriend00: Yes, I have optimizations. @Raynos: I've tried caching the live nodelist from "*" and it actually worsens performance. Odd, but true. –  minitech Nov 1 '11 at 2:16
add comment

1 Answer

up vote 3 down vote accepted

I have not done measurements to see how much this matters, but I can see in stepping through a simple selector operation in your library and in jQuery that you have two regex operations and a string trim whereas jQuery has only a single regex operation. You have a trim to remove extra whitespace, a regex to find the simple #selectorID and another regex for your unescape function. jQuery doesn't do the unescape or the trim (it uses the regex to handle extra whitespace).

Further, your unescape function uses a regex that is not precompiled so it has to be compiled from scratch each time.

Edit: I've now done some measurements.

Without modification, I find your function very similar in performance to jQuery in several different modern browsers (Firefox, Chrome, IE9).

If I remove the escape and the trim, your library benchmarks faster than jQuery here in each browser I tried: http://jsperf.com/sprint-selector-id-benchmark.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.