HTML5 Zone is brought to you in partnership with:

Gil Fink, Microsoft MVP, is an expert in Web development and Microsoft data platform. He works as a senior architect at Sela Group. He is currently consulting for various enterprises and companies, where he architects and develops Web and RIA-based solutions. He conducts lectures and workshops for developers and enterprises who want to specialize in infrastructure and Web development. He is also a co-author of several Microsoft Official Courses and training kits. You can read his publications at his blog: http://blogs.microsoft.co.il/blogs/gilf. Gil is a DZone MVB and is not an employee of DZone and has posted 151 posts at DZone. You can read more from them at their website. View Full User Profile

Quick Tip – JavaScript Statement Count Do Count

04.25.2013
| 2223 views |
  • submit to reddit

One of the things that we as developers don’t seem to mind about is the amount of statements that we write in our code. In JavaScript, the number of statements might affect the speed of the operations we want to perform. It is our responsibility to find statements that should be combined in order to decrease the execution time. This post include some examples of places to decrease the amount of statements.

Variable Declarations

One of the places that developers don’t care about is declaring variables. Lets look at a code with variable declarations:

var i = 0;
var j = 0;
var length = arr.length;

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:

var i = 0,
    j = 0,
    length = arr.length;

This is micro optimization but in large scale JavaScript applications such optimizations can be much more crucial.

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:

var arr = new Array();
arr[0] = 0;
arr[1] = 1;
 
var obj = new Object();
obj.name = "name";
obj.value = "value";

Those examples can be shortened in number of statements by using initialization using brackets or literal objects:

var arr = [0, 1];
 
var obj = {
   name: "name",
   value: "value"
};

Again this is micro optimization but any optimization count and limiting the number of statements means better performance.

Iterative Values

In loops we are using iterative values and increment/decrement them. Lets look at an example:

var i = 0;
while (i < 10) {
   doSomething(i);
   i++;
}

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:

var i = 0;
while (i < 10) {
   doSomething(i++);
}

Returning Evaluated Boolean Value

From time to time I see developers use the following if statement example to make it more comprehensive:

function checkSomething(value) {
    if (makeCheck(value)) {
       return true;
    }
    else {
       return false;
    }
}

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:

function checkSomething(value) {
    return makeCheck(value);
}

Pay attention that this is only an example of a pattern that I see people using which can be shortened.

Summary

The number of statements in your JavaScript code do count in your application performance. I know that all of the examples I showed are micro optimizations but in large scale JavaScript applications every micro optimization can help you create a faster application.




Published at DZone with permission of Gil Fink, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)