开发者

jQuery() slower than getElementById

开发者 https://www.devze.com 2023-01-10 08:55 出处:网络
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

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(search开发者_Python百科Win.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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消