Designing the Front-End
Posts tagged jQuery
Minimize jQuery object creation
Jul 4th
Willing to optimize apps to run the fastest posible, developers can sometimes asume (mostly after reading posts amateur developers write) that some optimizations are really better practices.
But unfortunately sometimes it might even slower your app.
sometimes these optimization’s are just for edge cases, (for instance only where there are 1000′s of nodes to manipulate)
It is very common to see code like this, where a new jQuery instance object ($) is created for every set of elements:
$("div").bind("click", function(){
$("#otherDiv").toggle();
})
Each time you call the $() you create a new instance of jQuery.
You can get away with creating as little as a few jQuery Object’s
Just start off with one jQuery instance.
Here is how we can optimize our javascript.
// define one root element
var appRoot = $("#appRoot");
appRoot.find(".someElement").bind("click", function(){
appRoot.find("#otherDiv").toggle();
});
Here he have one root element and we keep on looking (finding) from that root element.
But…
After making a few jsperf tests it turns out that it is not true. It is actually much slower.
See the stackoverflow thread for a detailed explanation.
Here’s an excerpt of the answer:
You need to consider that your test contains less than 10 divs or other html elements. The reason to write code like in the first example is to make the selector faster, but with the cost of additional method calls. Normaly the selector should be the more expensive of the two by far so the the gain would outweight the loss but with a DOM this small the selector will be very cheap no matter how you write it.
Sometimes over-optimizing jQuery causes script to run slower
Jul 3rd
Lately more and more developers are focusing on performance and it is a new hype in the javascript development world to optimize Javascript code.
Developers these days do not only care that their app works in all browsers etc. They will go the extra mile and ensure that the code is optimized and that the javascript execution time is the fastest it can be.
Recently i wrote a new jQuery plugin i tried to optimize the code as much as possible.
Looking at these 2 snippets:
$("div").width();
$("div").eq(0).width();
They return the same result.
Which one would be faster?
As you might know jQuery’s width() method returns the current computed width for the first element in the set of matched elements.
So I always thought why give jQuery the whole set just to get the first elements width, why not make it easier and pass only the first element in the collection by filtering the collection with .eq() to the width method.
It turns out that the width method itself handles it better.
See the results of the jsperf i put together.
The truth is, as @paulirish says “Don’t treat jQuery as a Black Box” always dig in to the source code and see what it does internally.
If you look in the jQuery’s fn.width source where you can see that jQuery filters out the first element the most native way it can and it does it right in the beginning of the function.
var elem = this[0];
Here are 2 videos in which paul digs into jQuery’s core.
10 things i learned from the jquery source
11 more things i learned from the jquery source