Over a million developers have joined DZone.

Quick Tip – JavaScript Statement Count Do Count

DZone's Guide to

Quick Tip – JavaScript Statement Count Do Count

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

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.

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.


Published at DZone with permission of Gil Fink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}