Best performance if-statements | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
0

# Best performance if-statements

Im wondering what the best way to make an if statement is. Im working on a project that is looping 10,000 times, so readibility is not a priority. Just performance if (5 > 6) { var h = true; } ^ has brackets {} if (5 > 6) var h = true; ^ no brackets var h = 5 > 6 ? true : false; ^ ternary operator

19th Feb 2022, 10:26 PM
ITDW
3 Antworten
+ 1
ITDW {} Doesn't effect on your performance. Unnecessary looping and unnecessary condition effect on your performance. That is just a way to write single line statement without using {} but if there are multiline statement then you cannot ignore to use {} because it denotes a block of code. Ternary operator doesn't make sense here because 5 > 6 would return either true or false so just h = 5 > 6; one more example: if (5 > 6) return true; else return false; this also doesn't make sense because you can directly do like this: return 5 > 6; // this will take less time rather than above
20th Feb 2022, 3:23 AM
A͢J
+ 1
That depends on the complexity and length of the conditional code and your personal preference. To be sure, you can use the {} ersion
19th Feb 2022, 10:30 PM
Lisa
+ 1
I would eliminate the if statement altogether and just assign the Boolean result. var h = 5>6
20th Feb 2022, 2:38 AM
Brian