I want to use data-src
attribute when screen.width > 767
and data-src-small
when screen.width < 768
. I have two methods in mind:
Method 1:
var src = "data-src";
if($(window).width() < 768) {
src = "data-src-small";
}
// do something with src variable.
Method 2:
if($(window).width() > 768) {
var src = "data-src";
}
else {
var src = "data-src-small";
}
// do something with src variable.
I have come across this situation twice. So I think it is is important to know which method is better performance wise as this situation may come across later too.
Edit : I don't mean this question to be specific to javascript. I mean in general which is fast, variable reassigning or an extra condition to be evaluated in else
? The same situation could be in C language too like this:
string salary;
...
...
salary = "LOW";
if(person == "RICH") {
salary = "HIGH";
}
Method2:
string salary;
...
...
if(person == "RICH") {
salary = "HIGH";
}
else {
salary = "LOW";
}
If the answer depends upon the compiler or language then please answer for Chrome V8 engine for javascript
language and gcc-4.9.2 compiler for C
language. Thanks