jquery - Costs of expanding an associative array in Javascript? -
because finding node jquery has important cost, built simple function
window.selectortoobject = {} function get(selector){ if(typeof selector === "undefined") homecoming null; if(selector in window.selectortoobject) homecoming window.selectortoobject[selector]; window.selectortoobject[selector] = $(selector); homecoming $(selector); }
so instead of doing $(selector)
get(selector)
.
after simple test, differences significant:
<div id="test"> <div id="child1"> <div id="child2"> <div id="child3"></div> </div> </div> </div>
now worry size of associative array grow @ each new get()
.
is there costs of expanding size of associative array in javascript?
this help if making big number of calls same selector. if doing this, yes help, should caching accesses in locally scoped variable if same anyway.
however, break selector in case of dynamic content. if phone call get('.mydiv')
, add together <div class="mydiv"></div>
later, get()
homecoming object latest div
missing.
as alluded to, there memory cost here. trading speed memory footprint. in general, not issue, neither performance of jquery selectors unless doing huge number of selector calls.
practically speaking, uncertainty have need this. don't go optimization unless issue. should have mind toward writing code, resist urge over-optimize.
javascript jquery
No comments:
Post a Comment