jQuery Howto blog has many posts on your javascript and jQuery code performance. If you have read the last performance post named “5 easy tips on how to improve code performance with huge data sets in jQuery” then you probably got an idea that it’s better to work with arrays for better javascript performance.
The only problem is that jQuery returns an object not an array when you select elements. Consider you have an object with lots of entries and you have to perform some manipulations that are available in javascript array such as reverse
, sort
, join
, etc. Using built in methods is much faster then those you might write yourself. So the best thing would be converting your objects to arrays and jQuery provides utility method that does exactly this – jQuery.makeArray(obj)
.
// From jQuery Documentation var arr = jQuery.makeArray(document.getElementsByTagName("div")); arr.reverse(); // use an Array method on list of dom elements $(arr).appendTo(document.body);