3 ms·
Are there speed gains to be had by wrapping the a,b elements in the jQuery example only once instead of twice? $("div").sort(function(a, b) { var $a =
by WesleyJohnson 13y ago
Are there speed gains to be had by wrapping the a,b elements in the jQuery example only once instead of twice?
$("div").sort(function(a, b) {
var $a = $(a);
var $b = $(b);
return $a.width() * $a.height() - ($b.width() * $b.height());
});
- Schlaefer 13y agoNot really: http://jsperf.com/d97b341f-cfc1-4057-bdc9-60e80adb5cf6 http://jsperf.com/d97b341f-cfc1-4057-bdc9-60e80adb5cf6
- simonw 13y agoThere is however a significant performance improvement to be had by optimising the inner loop, which is what any good programmer would do first here: http://jsperf.com/d97b341f-cfc1-4057-bdc9-60e80adb5cf6/2 http://jsperf.com/d97b341f-cfc1-4057-bdc9-60e80adb5cf6/2
- k3n 13y agoWritten sanely, it's only marginally slower; it's so close that it's inconsequential for 99% of uses.
- ldng 13y agoYou still have to download 81Kb of code whereas native is just that, native ;-) It's a trade-off. Depends which browser uses your target/customer.
- Schlaefer 13y agoThis optimization is actually replacing jQuery in the inner loop with native calls. It was my impression that the OP suggested that this could be performed more efficiently using jQuery.