dom - jQuery: What is more efficient? Many ID specific selectors, or one "Contains Prefix Selector" -


i have snippet of javascript in project caches array of jquery objects (anchor tags) on dashboard:

$.extend(cache, {     dashboard : {         buttons : [             $('#dash-new-lead'),               $('#dash-jobs-calendar'),             $('#dash-view-lead'),              $('#dash-sales-reports'),             $('#dash-search-leads'),           $('#dash-new-job'),             $('#dash-dispatch-jobs'),          $('#dash-dispatch-reports'),             $('#dash-manage-employees'),       $('#dash-manage-trucks'),             $('#dash-finalize-jobs'),          $('#dash-payment-profiles'),             $('#dash-employee-statements'),    $('#dash-company-statements'),             $('#dash-finance-reports'),        $('#dash-admin-sources'),             $('#dash-admin-statuses'),         $('#dash-admin-companies'),             $('#dash-admin-groups'),           $('#dash-admin-users'),             $('#dash-admin-dispositions'),     $('#dash-search-jobs'),             $('#dash-jobs-calendar-dispatch'), $('#dash-new-lead-dispatch'),             $('#dash-finance-notices')         ]     } }); 

each link styled later (using $.each) button. each link gets unique jquery ui icon (hence id's instead of class selector). depending on user's access level links may or may not exist in dom.

i'm wondering if more efficient use jquery's contains prefix selector:

$.extend(cache, {     dashboard : {         buttons : $('a[id|="dash-"]')     } }); 
  1. pro: fewer references jquery object = faster
  2. con: jquery cannot use document.getelementbyid = slower

it'll depend on browser, you'll need test sure.

because 'a[id|="dash"]' (notice removed -) appears valid queryselectorall() selector, browsers support method (which includes ie8) should have performance.

because you're including tagname, browsers don't support queryselectorall() (i believe) use getelementsbytagname(), filter applied <a> elements finds.

it should if you're able limit query narrower context document.

note according test, performance of getelementbyid() isn't terribly exciting in ie6 , ie7. , again ie8 support queryselectorall(), though wouldn't bad make sure succeeds particular selector. if not, query default sizzle's engine.


here's test using selector directly through queryselectorall(). can run in different browsers see supported.


note getelementsbyclassname() isn't supported before ie9, doubt using class offer great performance improvements on 'a[id|="dash"]'. exception being firefox3, support getelementsbyclassname(), , not queryselectorall().


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -