Hacker News new | past | comments | ask | show | jobs | submit login

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());
  });




There 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


This 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.


Written sanely, it's only marginally slower; it's so close that it's inconsequential for 99% of uses.


You 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.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: