views:

37

answers:

2

Hi folks,

I develop for a web app startup and I come across the following code quite often.

<div class="items container">
    <div class="item">
        <div class="property propertyA">Some stuff</div>
    </div>
</div>

Our typical jQuery selector looks pretty much similar to:

$("div.items.container > div.item > div.property.propertyA").map(doStuff());

The thing is that we call class selector VERY often up to the point where most of the time spent executing JS is spent running jQuery's internal curCSS method.

While we might only have one items container it might contains several hundreds of items each with dozens of properties. Actually there is always around 5K+ html tags present on the page at any given time.

We are bundling more and more logic in the client side to accomodate new features. I feel this won't be sustainable for long as some browsers are already showing the deadly "your page is unresponsive" message under some conditions. We are thinking about refactoring the entire client side pretty soon.

I was thinking about implementing some new XHTML tags in a new xml namespace so it could look like:

<items>
    <item>
        <propertyA>Some Stuff</div>
    </item>
</items>

I would then be able to select the following way:

$("items > item > propertyA")

or directly

$("propertyA")

Correct me if I'm wrong but by getting rid of my slow CSS selectors in favor of some "getElementsByTagName" could save me a lot of cycle here while making my css cuter and my overall code neater. Would such a solution work?

BTW: here we run with the assumption that IE does not exists and firefox 3+ || Chrome 3+ is our target audience.

A: 

As long as you make the custom xlmns it is a great idea, anything that trims down your development time is a great idea. Also, is this for a closed enviroment? or are you just turning off IE Support?

Rixius
additionally, even HTML can implement custom atributes, HTML5 is standardizing data-* you can add data-keyword, and put an unlimited number of custom attributes for use with js/css
Rixius
Nope, not closed environment. We are targeting "the wild".. I'm just lucky enough to have a IE user-base <5%. We implemented google chrome frame yesterday to help these 5% folks get in the 21st century.. Think the business prefers to spend time implementing HTML5 feature to differentiate from the competition rather than supporting dying technologies.
matdumsa
That's pretty great, I operate on a closed enviroment, and we're about to push latest Stable Chrome on everyone. Needless to say, I'm stoked.
Rixius
+1  A: 

I don't know if what you're suggesting is really necessary or will improve speed that much. However, if you know that the "property" or "propertyA" classes will always be inside the "item" class, then it would be more efficient to just use .property or .propertyA as the selector.

If your .items.container element is unique to the page, consider using an ID on it instead. Then jQuery will first select the ID and only look inside that for the items. And don't add the tag names to the selection query unless you have other elements you want to exclude (like <p class="propertyA">...</p>).

If you are repeatedly calling the selector, you should cache the result to prevent repeating the search (although jQuery 1.4 does this for you I believe). Something like:

var $properties = $(".items.container > .item > .property.propertyA");
$properties.map(doStuff());
//later on...
$properties.map(doStuff());
DisgruntledGoat
Thanks DisgruntledGoat, Thing is that some classes like "property" are there for styling while some are there for the purpose of being selected by JS.Also I'm already caching as many jQuery object as I can.also items.container might be unique or not, depends on the user's context.
matdumsa
In that case I would try just keeping the selectors that you need for JS and not CSS. My main point was meant to be that a `items > item > propertyA` selector is unlikely to be any more efficient than `.items > .item > .propertyA`.
DisgruntledGoat