views:

39

answers:

2

Is there a difference in performance (I am not asking about readability) if I condense my code into one line versus two?

For example:

var slide = 'images/' + n + '.png';
$('img').attr('src',slide);

versus

$('img').attr('src','images/' + n + '.png');

Personally, I like fewer lines of code. Often, I am the only one reading my code, so communicating intent is not as important.

I am curious if the Javascript interpreter executes one of the above options faster (even though this is a classic micro-optimization example).

+5  A: 

No difference in rendering performance, at all.

Daniel Vassallo
it certainly makes it harder to debug in some cases though.
NickLarsen
A: 

Micro-optimization doesn't half say it. :-)

The answer will depend a lot on the interpreter involved. I wouldn't be surprised if IE's interpreter had a very very very very very very (...continue saying "very" for a while...) very slight, impossible to detect difference. Chrome's V8, on the other hand, is certain not to.

In real terms, though? No, no difference at all.

T.J. Crowder