views:

71

answers:

1

Hi,

is it better to do this (regarding performance, not readability...):

$('a.updateCartButton').click(function() {
  $('form[name=updateCartForm]').attr('action', $(this).attr('href') + '#' + $('img[id^=iconUpdateArticle]').attr('id')).submit();
  return false;
});

or this:

$('a.updateCartButton').click(function() {
  var actionHref = $(this).attr('href');
  var buttonId = $('img[id^=iconUpdateArticle]').attr('id');
  $('form[name="updateCartForm"]').attr('action', actionHref + '#' +buttonId).submit();
  return false;
});

Maybe someone could also explain to me how I can write a test case for something like this in jQuery Tester? Then I don't have to ask questions like this in the future... ;)

Thanks!

+1  A: 

Either one is fine, you're talking about the performance of variable assignment, quite possibly the fastest feature in any language. The difference between:

var a = 1;
var b = 2;
return a + b;

versus:

return 1 + 2;

Should be close to nil for all modern day languages.

jakemcgraw
Thanks for the fast answer, Jake. Interesting: I would have thought that return 1 + 2 is somewhat faster than return a + b...
north
There are many naive JavaScript runtimes out there, so the difference *might* be measurable in your example. However, OP's code is complex enough that I'm sure the cost of naming the local variables would be lost in the noise.
ephemient
Exactly my point, feel free to mark it as the correct answer if you like ;-)
jakemcgraw