Quick Tip – JavaScript Statement Count Do Count

April 20, 2013

no comments

Quick Tip – JavaScript Statement Count Do Count

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) {

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) {

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.


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.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>