Www l Www asearchs Feet p Feet esearchi Feet Www searchl Lightsoffnow s Www Feet i Feet hasearchssearchasearchct Feet Lg Tag tsearcho
f Lightsoffnow o Tag Feet
i Lightsoffnow ht Feet o Tag fn1w Lightsoffnow e1(a
d Lightsoffnow r Tag msearchmsearche Www Www o Feet d1scsearchnsearch searchr Feet m Www a
Dsearch:
var active_light = $('#traffic_light input.on'); Note: The class selector is among the slowest selectors in jQuery; in IE it loops through the entire DOM. Avoid using it whenever possible. Never prefix an ID with a tag name. For example, this is slow because it will loop through all
var content = $('div#content'); Along the same lines, it is redundant to descend from multiple IDs:
var traffic_light = $('#content #traffic_light'); Get in the habit of saving your jQuery objects to a variable (much like our examples above). For example, never (eeeehhhhver) do this:
$('#traffic_light input.on).bind('click', function(){});
$('#traffic_light input.on).css('border', '3px dashed yellow');
$('#traffic_light input.on).css('background-color', 'orange');
$('#traffic_light input.on).fadeIn('slow'); Instead, first save the object to a local variable, and continue your operations:
var $active_light = $('#traffic_light input.on');
$active_light.bind('click', function(){});
$active_light.css('border', '3px dashed yellow');
$active_light.css('background-color', 'orange');
$active_light.fadeIn('slow'); Tip: Since we want to remember that our local variable is a jQuery wrapped set, we are using $ as a prefix. Remember, never repeat a jQuery selection operation more than on
If you intend to use the jQuery result object(s) in another part of your program, or should your function execute more than on
// Define an object in the global scope (i.e. the window object)
window.$my =
{};
function do_something()
{}
The previous example can also be accomplished like this:
var $active_light = $('#traffic_light input.on');$active_light.bind('click', function(){})
.css('border', '3px dashed yellow')
.css('background-color', 'orange')
.fadeIn('slow'); This allows us to write less co
jQuery allows us to run additional selector operations on a wrapped set. This reduces performance overhead on subsequent selections since we already grabbed and stored the parent object in a local variable.
<div id="content">
<form method="post" action="">
<h2>Traffic Light</h2>
<ul id="traffic_light">
<li><input type="radio" class="on" name="light" value="red" /> Red</li>
<li><input type="radio" class="off" name="light" value="yellow" /> Yellow</li>
<li><input type="radio" class="off" name="light" value="green" /> Green</li>
</ul>
<input class="button" id="traffic_button" type="submit" value="Go" />
</form>
</div>
For example, we can leverage sub-queries to grab the active and inactive lights and cache them for later manipulation.
var $traffic_light = $('#traffic_light'),
$active_light = $traffic_light.find('input.on'),
$inactive_lights = $traffic_light.find('input.off'); Tip: You can declare multiple local variables by separating them with commas ¨C save those bytes!
The basic idea here is to create exactly what you need in memory, and then update the DOM. This is not a jQuery best practice, but a must for efficient JavaS
var top_100_list = [...], // assume this has 100 unique strings
$mylist = $('#mylist'); // jQuery selects our <ul> element
for (var i=0, l=top_100_list.length; i<l; i++)
{}
Instead, we want to create the entire set of elements in a string before inserting into the DOM:
var top_100_list = [...], // assume this has 100 unique strings
$mylist = $('#mylist'), // jQuery selects our <ul> element
top_100_li = ""; // This will store our list items
for (var i=0, l=top_100_list.length; i<l; i++)
{}
$mylist.html(top_100_li);
Even faster, we should always wrap many elements in a single parent node before insertion:
var top_100_list = [...], // assume this has 100 unique strings
$mylist = $('#mylist'), // jQuery selects our <ul> element
top_100_ul = '<ul id="#mylist">'; // This will store our entire unordered list
for (var i=0, l=top_100_list.length; i<l; i++)
{}
top_100_ul += '</ul>'; // Close our unordered list
$mylist.replaceWith(top_100_ul);
If you do the above and are still concerned about performance:
Unless told otherwise, every event (e.g. click, mouseover, etc.) in JavaS
$('#myList li).bind('click', function(){}); Instead, we should listen for the click event at the parent level:
$('#myList).bind('click', function(e){}
}); The parent node acts as a dispatcher and can then do work based on what target element triggered the event. If you find yourself binding on
Although jQuery fails nicely if it does not find any matching elements, it still takes time to look for them. If you have on
<script type="text/javascript>
mylib.article.init();
</script>
</body>
If your page template includes any variety of modules that may or may not be on the page, or for visual reasons you need them to initialize sooner, you could place the initialization function immediately after the module.
<ul id="traffic_light">
<li><input type="radio" class="on" name="light" value="red" /> Red</li>
<li><input type="radio" class="off" name="light" value="yellow" /> Yellow</li>
<li><input type="radio" class="off" name="light" value="green" /> Green</li>
</ul>
<script type="text/javascript>
mylib.traffic_light.init();
</script>
Your Global JS library would look something like this:
var mylib =