If + override variable vs. if + else. Which is better, performance wise?

I want to use the attribute data-srcwhen screen.width > 767and data-src-smallwhen screen.width < 768. I have two methods:

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 encountered this situation twice. Therefore, I think it is important to know which method works best, since this situation may come later.

Edit: I don't want this question to be javascript specific. In general, I mean, is this a quick, variable reassignment or an additional condition for evaluating in else? The same situation can be in C as follows:

 string salary;
 ...
 ...
salary = "LOW";
if(person == "RICH") {
  salary = "HIGH";
}

Method 2:

string salary;
...
...
if(person == "RICH") {
    salary = "HIGH";
}
else {
    salary = "LOW";
}

, Chrome V8 javascript gcc-4.9.2 C.

+4
1

3- , js css

<picture>
  <source media="(min-width: 768px)" srcset="https://dummyimage.com/600x400/000/f00&text=big">
  <source media="(max-width: 768px)" srcset="https://dummyimage.com/300x200/000/fff&text=small">

  <!-- fallback if browser don't understand picture element -->
  <img src="https://dummyimage.com/600x400/000/0f0&text=small" alt="kitten-curled">
</picture>
Hide result
+3

Source: https://habr.com/ru/post/1661865/


All Articles