A month ago I got excited about node.js. Watched two presentations, installed it, ran a few tests, and then ran out of steam for lack of a real project idea. Hate when that happens.

At the same time I also got excited about learning advanced Javascript concepts since it would no doubt help me with node.js, so I decided to investigate jQuery and see what makes it tick. And what better way to really investigate jQuery than to try to build something similar on my own? Actually, that’s only part of the story: I wanted to use jQuery on an XML document, but discovered that the namespaces render it useless. I saw no reason why it shouldn’t work. If I couldn’t fix jQuery, I’d have to build something similar myself.

I couldn’t make sense of jQuery enough to fix it, so Szelector was born. And really that’s all it is at the moment: a selector. In other words, it parses a CSS3 style selector string and returns elements from the DOM that match the string. I haven’t finished attribute matching, but hierarchy-related fetching mostly works.

My code changed a lot as I learned about optimization (work from references whenever possible, reduce the number of function calls, optimize loops as appropriate, etc) and got a better understanding of the problem space. I even used the sizzle benchmarking setup to test my project against sizzle, jQuery, dojo, mootools. In some cases my project is faster, but mostly sizzle is blazing. And here’s why:

Gecko 1.5.1 brought querySelectorAll() to the JavaScript API, which sizzle uses whenever it can. On startup, if it can, sizzle swaps out its parser + selector logic with querySelectorAll() and friends. Cheater! The browser’s selector code will always be much faster, so it makes sense. But still, I was surprised to find that going on under the hood.

Trying to keep this high level … what else have I gained from this project?

  • I got familiar with Chrome’s JavaScript debugger
  • Can now recognize namespace pollution avoidance in JavaScript
  • Came to appreciate once-again just how much jQuery does for me.

At this point it’s been a month since I’ve touched the project. Not what I intended to happen, but there’s a lot of mental energy necessary for something like this, and my brain is needed elsewhere for the time being.

If you have any questions, feel free to ask.