技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機純凈版,64位旗艦版,綠色軟件,免費軟件下載基地!

當前位置:主頁 > 教程 > 服務(wù)器類 >

jQuery查找dom的幾種方法效率對比

來源:技術(shù)員聯(lián)盟┆發(fā)布時間:2017-10-28 06:13┆點擊:

關(guān)于這個問題的產(chǎn)生由于我們前端組每個人的編碼習慣的差異,最主要的還是因為代碼的維護性問題。在此基礎(chǔ)上,我對jQuery源碼(1.11.3)查找dom節(jié)點相關(guān)的內(nèi)容進行了仔細的查閱,雖然并不能理解的很深入 。。同時基于對瀏覽器console對象的了解產(chǎn)生了一系列之后的問題和分析,對jQuery最常用的三種dom查找方式進行了一個查找效率和性能方面的比較分析。

首先我們要用到的是console.time()和console.timeEnd()這兩個成對出現(xiàn)的console對象的方法,該方法的用法是將他們兩者之間的代碼段執(zhí)行并輸出所消耗的執(zhí)行時間,并且兩者內(nèi)傳入的字符串命名須統(tǒng)一才能生效,例如:

console.time('Scott'); console.log('seven'); console.timeEnd('Scott'); seven Scott: 0.256ms

代碼段中三處一致才是正確的用法。

正文

接下來我們來討論我們常用的jQuery查找dom方式:

1.$(‘.parent .child'); 2.$(‘.parent').find(‘.child'); 3.$(‘.child','.parent');

其中方式1和方式3都是基于jQuery的selector和context的查找方式,既我們最常用的jQuery()或者$() ,
詳細即為:

jQuery = function( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' // Need init if jQuery is called (just allow error to be thrown if not included) return new jQuery.fn.init( selector, context ); }

基于jQuery(1.11.3)70行處,為該方法的入口,他做的所有事情就是創(chuàng)建了一個jquery.fn上的init方法的對象,我們再來細看這個對象是什么:

init = jQuery.fn.init = function( selector, context ) { var match, elem; // HANDLE: $(""), $(null), $(undefined), $(false) if ( !selector ) { return this; } // Handle HTML strings if ( typeof selector === "string" ) { if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) { // Assume that strings that start and end with <> are HTML and skip the regex check match = [ null, selector, null ]; } else { match = rquickExpr.exec( selector ); } // Match html or make sure no context is specified for #id if ( match && (match[1] || !context) ) { // HANDLE: $(html) -> $(array) if ( match[1] ) { context = context instanceof jQuery ? context[0] : context; // scripts is true for back-compat // Intentionally let the error be thrown if parseHTML is not present jQuery.merge( this, jQuery.parseHTML( match[1], context && context.nodeType ? context.ownerDocument || context : document, true ) ); // HANDLE: $(html, props) if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) { for ( match in context ) { // Properties of context are called as methods if possible if ( jQuery.isFunction( this[ match ] ) ) { this[ match ]( context[ match ] ); // ...and otherwise set as attributes } else { this.attr( match, context[ match ] ); } } } return this; // HANDLE: $(#id) } else { elem = document.getElementById( match[2] ); // Check parentNode to catch when Blackberry 4.6 returns // nodes that are no longer in the document #6963 if ( elem && elem.parentNode ) { // Handle the case where IE and Opera return items // by name instead of ID if ( elem.id !== match[2] ) { return rootjQuery.find( selector ); } // Otherwise, we inject the element directly into the jQuery object this.length = 1; this[0] = elem; } this.context = document; this.selector = selector; return this; } // HANDLE: $(expr, $(...)) } else if ( !context || context.jquery ) { return ( context || rootjQuery ).find( selector ); // HANDLE: $(expr, context) // (which is just equivalent to: $(context).find(expr) } else { return this.constructor( context ).find( selector ); } // HANDLE: $(DOMElement) } else if ( selector.nodeType ) { this.context = this[0] = selector; this.length = 1; return this; // HANDLE: $(function) // Shortcut for document ready } else if ( jQuery.isFunction( selector ) ) { return typeof rootjQuery.ready !== "undefined" ? rootjQuery.ready( selector ) : // Execute immediately if ready is not present selector( jQuery ); } if ( selector.selector !== undefined ) { this.selector = selector.selector; this.context = selector.context; } return jQuery.makeArray( selector, this ); }

基于jQuery(1.11.3) 2776行處,該方法比較長,我就來大概說一下我對這個方法的了解:這里主要就是做了先對selector的判斷,在判斷完后,查找context如果存在就繼續(xù)做對有context存在情況的處理,沒有則進行沒有context情況的處理,而方式1和方式3:

1.$(‘.parent .child'); 3.$(‘.child','.parent');

他們都要進入相同的判斷步驟,即上面簡要說明的判斷流程,等到1和3判斷完后所花費的時間基本差不多,但是1內(nèi)部的選擇器還需要花費時間去進行sizzle相關(guān)查找,得出:

方式1. $(‘.parent .child'); 走完流程花費的時間:a;

方式3. $(‘.child','.parent'); 走完流程花費的時間:a; 幾乎已經(jīng)找到dom節(jié)點

方式1. $(‘.parent .child'); sizzle相關(guān)查找選擇器.parent .child花費的時間:b;

所以得出初步結(jié)論:

方式3. $(‘.child','.parent');花費的時間:a;

方式1. $(‘.parent .child');花費的時間:a + b;

方式3優(yōu)于方式1

接下來我們來看實際的運行結(jié)果:

jQuery查找dom的幾種方法效率對比 三聯(lián)