admin管理员组文章数量:1277899
I want to ask about optimisation in JavaScript if else
statement.I have the code like this
if ( thisIsTrue )
printMe("someMessage");
And the code can be optimisation like this
thisIsTrue && printMe("someMessage");
The question is what the code of thisIsTrue && printMe("someMessage");
only works in statement who has been return true.
What if the return is false?
I want to ask about optimisation in JavaScript if else
statement.I have the code like this
if ( thisIsTrue )
printMe("someMessage");
And the code can be optimisation like this
thisIsTrue && printMe("someMessage");
The question is what the code of thisIsTrue && printMe("someMessage");
only works in statement who has been return true.
What if the return is false?
Share Improve this question edited Jun 7, 2012 at 23:27 Igor F. 2,6992 gold badges33 silver badges41 bronze badges asked Jan 5, 2012 at 8:07 viyancsviyancs 2,3394 gold badges40 silver badges72 bronze badges 1-
2
What about
!thisIsFalse && printMe("someMessage")
. However, this is not really an optimization; all it does is make the code more difficult to read. I would go with your first example, and add braces too. – Peter Commented Jan 5, 2012 at 8:11
5 Answers
Reset to default 13You should avoid turning your code into a puzzle either for someone else, or for the future you. There is nothing to be gained from mixing your conditions with your output. It's not going to "optimize" anything, and it will cost you lots of time in the future as you try to debug this.
I would argue you should err on the side of MORE verbosity not less.
thisIsTrue ? printMe("is true") : printMe("is false");
Although this is not optimisation and not suggested but i would explain the details.
in a bination of operands like A && B
, 1st
the operand A
will be executed and if it return true
the second operand
will be executed otherwise it will be terminated because if the 1st operand
is true
the result
will depends on the second operand
else if the 1st
is false
result
will surely be false
irrespective of the second operand so it doesnot execute the second one. so
true && alert("someMessage"); will show alert
and
!false && alert("someMessage....."); will also show
binding the above two, you can write is as
condition ? alert("someMessage") : alert("someMessage.....");
fiddle : http://jsfiddle/9q2jC/1/
This is hardly an optimisation (in terms of performance).
Also, these two forms are not identical.
The latter is an expression, while the former is a statement.
If you have a series of If-Then-Else statements, you should try your best to organize them from most likely to least likely. This way the program doesn't have to traverse all the unlikely conditions to get to the likely ones, which will be called more often.
A monly used tactic to wring whatever overhead might be left out of a large group of simple conditional statements is replacing If-Then-Else's with Switch statements. This is a tough one to qualify. Performance varies widely, although you almost always e out with some kind of improvement, even if it is small. As always, proper code design is much more important. See there are good test cases with switch and if elseif else
本文标签: Javascript if else statement optimisationStack Overflow
版权声明:本文标题:Javascript if else statement optimisation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741301693a2371124.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论