|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Features 101 on Prototype CSS Selectors
Performance implications of certain CSS Selectors are not specific to a certain JavaScript Library like Prototype
By: Andreas Grabner
Nov. 30, 2009 03:00 PM
Java Developer Magazine on Ulitzer Performance implications of certain CSS Selectors are not specific to a certain JavaScript Library like Prototype. I recently blogged about the internals of CSS Selectors in jQuery. The same holds true for every JavaScript library that offers CSS Selectors. Certain lookups can be done by using the native browser functions like getElementById or getElementsByTagName. Lookups by class name are not natively supported in IE and are therefore implemented in JavaScript by iterating through all elements in the DOM - which is obviously much slower than a native implementation. Expensive Prototype CSS Selector $$("[id^=contentElement]") -> up to 6 seconds on a single page to find all elements that have an id starting with contentElement $$("div[class=contentDiv]") -> up to 2.3 seconds on a single page to find all div tags with the class contentDiv Selector Expressions
Additionally to these we have two selectors that allow you to query for elements with attribute values containing hyphen or space separated list values.
When using expressions like the ones above (except id=contentElement) Prototype needs to iterate through all DOM elements and manually match the DOM's attribute value with the expected value as there is no native implementation available for this feature by the browser. Depending on the size of your DOM this can cause a huge performance impact. Sample 1 - Find elements which attributes that start with a certain text In this example the CSS Expression caused 191454 interactions with the DOM. Expensive CSS Selector causing 190k DOM Calls There are multiple solutions to this problem. One is to find a better way to identify these elements. Looking at the HTML document showed me that all these contentItemContainer elements were in fact DIV tags. Changing the query to $$("div[id=^contentItemContainer]") would have caused Prototype to use getElementsByTagName("div") which would have only returned 178 elements instead of 3184. In case these elements have different tags this first approach wouldn't work. Limiting the number of DOM elements on the page would be another option to speed up iterating through all elements. Sample 2 - Find elements by class name What was surprising to me was that the query $$("div[className=contentDiv]") still triggered many more DOM interactions than I expect. Using the "div" tag in the expression solves the problem that not all DOM elements are iterated but just the DIV tags using the native getElementsByTagName to get these elements. On those returned DIV elements Prototype defines it's extension methods like previousSiblings, nextSiblings, match, up, down, ... In my case I had 618 DIV tags. On each of these objects Prototype registered 57 extension methods. This "registration" in the end took most of the time of the item lookup. The query div[className=contentDiv] caused a total of 42415 DOM interactions even though I only had 618 DIV tags on the page where about 200 matched the passed className. I am wondering if there is a way to configure Prototype to not register the additional extension methods in case they are not needed on a particular web page. Any thoughts on this? Conclusion Related reading:
SOA World Latest Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||