Best performance if-statements | Sololearn: Learn to code for FREE!

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

2/19/2022 10:26:22 PM

ITDW

4 Answers

New Answer

+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

+1

That depends on the complexity and length of the conditional code and your personal preference. To be sure, you can use the {} ersion

+1

based on experience I'd either go for ternary operators or the first variant. Too often did I see loops or conditionals that were effective for one following line but not _later additions_. For the cost of typing of two additional characters you're moving from the harm's way.

+1

I would eliminate the if statement altogether and just assign the Boolean result. var h = 5>6