views:

57

answers:

1

I was running some performance tests to see if i could use jQuery directly instead of Ext's wrapper. To start with i wanted to compare jQuery(#id) to doc.getElementById, but i must be doing something wrong, since jQuery(#id) is awful slower.

var searchDoc = searchWin.document;
var jqSearchDoc = jQuery(searchWin.document);
for (var i=0; i<500; i++){
    var temp = jqSearchDoc.find('#myID'); //takes 1100ms
    //var temp = jQuery(searchDoc.getElementById('myID')); //takes 3ms
}

Any idea why the uncommented line is so much slower? and how to rewrite it?

+3  A: 

getElementById is a native method. jQuery is not. jQuery does things under the hood to make up for browser inconsistencies. It does getElementById after doing regexp matching and whatnot.

Naturally, jQuery is slower because it's a wrap around.

jQuery would be considered fast when compared with other frameworks, since it's a framework. You can't compare a browser native function to a framework that abstracts native methods, native methods will always be faster.

meder