Svetlo - One Line JavaScript Selector Engine

Tuesday, April 12, 2011 { 7 Comments }

Responses to “Svetlo - One Line JavaScript Selector Engine”

I'm working on one new mini JavaScript Framework for building mobile apps.Probably the most critical part of any JavaScript Framework is the selector engine. Naturally the faster is the selector engine the faster will be all the Framework.Most of the modern selector engines are build upon querySelectorAll because it gives the best flexibility. Little problem with querySelectorAll is the speed.Traditionally getElementById is much faster then querySelectorAll, but things are changing. Opera 11 has super optimized querySelectorAll and probably all the browsers in the future will try to speed optimize querySelectorAll.For my (not yet published) JavaScript Framework I've used this code for the selector engine (codename Svetlo).// Svetlo :What this code does is putting the querySelectorAll results "toArray" and that helps the speed optimization. The code is not new I think the first time I saw it on John Resig blog I'm not even sure that this line of code can be called selector engine, but I think that it can be helpful to someone and is good baseline for writing JS applications that doesn't need to run in IE. Hint: Web apps for iPhone and Android.I did some performance test.The test are based on this principle http://jsperf.com/nodelist-vs-array-iteration If my testing logic is correct result show that this selector engine is faster then the native querySelectorAll and equal like native getElementById method.If my testing logic is correct result show that this selector engine is faster then the native querySelectorAll and equal like native getElementById method.This solution will run on of Safari, Chrome, Firefox, Opera and doesn't support IE.My intention was to build JS Framework for mobile webkit browsers(iOS, Android).So if you building web app for iPhone, iPad, Android this selector engine can help you, if you need to support IE this selector engine is completely useless.Other super small selectors:Ben Alman QSA library Dan Heberden Quewery Dustin Diaz Qwery All comments and suggestion about this small selector will be appreciated!