One of the places that developers don’t care about is declaring variables. Lets look at a code with variable declarations:
This code block shows the declaration of 3 variables in 3 different statements. This is a little expensive since we can declare all the variables in only one var statement:
Arrays and Object Literals
Another place that you might be tempted to use a lot of statements is initialization of arrays or objects. Here is an example of wasting a lot of statements:
Those examples can be shortened in number of statements by using initialization using brackets or literal objects:
Again this is micro optimization but any optimization count and limiting the number of statements means better performance.
In loops we are using iterative values and increment/decrement them. Lets look at an example:
The statements in the loop can be shortened using only one statement which will do the same thing because of the nature of increment operator:
Returning Evaluated Boolean Value
From time to time I see developers use the following if statement example to make it more comprehensive:
In the code we check if a value is evaluated to true and then returning true. This is wasteful. The previous code can be shortened to:
Pay attention that this is only an example of a pattern that I see people using which can be shortened.