Essentials of Node.js

## Part-4 Node.js Execution Time

Brief Introduction

In this tutorial, we learn a simple method to calculate EXECUTION time of a nodejs program. Note that in the following text, we are using mostly synchronous functions. When working with asynchronous functions,
we may need to slightly change the technique. We'll look at that when we write asyn functions.

Step-1 Write functions

It's easy to write functions in Node.js and if you have ever programmed in javascript, you probably already know how to do it. Typical function signatures look like the following:

```
/**
Function Signature in Node.js.
words in CAPITALS are place holders and are different for different functions.
*/
function FUNCTION_NAME(var1, var2 . . . varN)
{
STATEMENT-1;
STATEMENT-2;
.
.
.
STATEMENT-N;
return RESULT;
}
// How do we call above function >>
var result = FUNCTION_NAME(var1, var2 . . . varN);
// Another way of doing the same thing is >>
var MY_FUNCTION = function(var1, var2 . . . varN) {
STATEMENT-1;
STATEMENT-2;
.
.
.
STATEMENT-N;
return RESULT;
}
// It is also called in the same fashion >>
var result = FUNCTION_NAME(var1, var2 . . . varN);
```

Let's teach mathematics to node

Let's write a function which does the following::

Takes 2 non-zero numbers A and B as input, and return the total sum of addition, subtraction, multiplication and division of A and B. Following is the code for this function along with informative comments where necessary.

Takes 2 non-zero numbers A and B as input, and return the total sum of addition, subtraction, multiplication and division of A and B. Following is the code for this function along with informative comments where necessary.

```
/**
@name: TotalSum.js
@description: A Simple Program in Node.js to find sum of addition + subtraction + multiplication + division of 2 numbers.
@author: @rishabhio
*/
var totalSum = function(A,B){
var addition = A + B;
var subtraction = A - B;
var multiplication = A * B;
var division = A / B;
var total_sum = addition + subtraction + multiplication + division;
return total_sum
}
```

Back to basics

Now our next task is to run above function 5 times with different set of input values and calculate the execution time of the program. We'll use Date-Time methods we learnt in NodeJS-Terminal tutorial. You may want to go back and look at it if required. Following is the complete code for our program TotalSum.js

```
/**
@name: TotalSum.js
@description: A Simple Program in Node.js to find sum of addition + subtraction + multiplication + division of 2 numbers.
@author: @rishabhio
*/
var totalSum = function(A, B){
var addition = A + B;
var subtraction = A - B;
var multiplication = A * B;
var division = A / B;
var total_sum = addition + subtraction + multiplication + division;
return total_sum
}
// Initialize the start_time of program
var date1 = new Date();
var start_time = date1.getTime();
// call the function 5 times and log results on the console.
console.log(totalSum(5,4));
console.log(totalSum(12,23));
console.log(totalSum(100,90));
console.log(totalSum(123.43,50.23));
console.log(totalSum(69593,2342));
// Find out the end time of program
var date2 = new Date();
var end_time = date2.getTime();
// Find execution time by subtracting start from end
var execution_time = end_time - start_time;
console.log("Time to execute program: " +execution_time);
```

Execute the code file

Use the node command from windows command prompt or Linux shell to execute your code file.

```
you@yourpc ~
$ node TotalSum.js
31.25
300.5217391304348
9201.111111111111
6449.206196436392
163126021.7152007
Time to execute program: 23ms
```

So 23ms is the time it took to execute our code. This technique can prove to be a very useful technique when we want to test the time performance of our program. Note that execution time for same script
will be different on different machines and under different circumstances. Therefore while measuring the performance, it's always advisable to keep a check of the execution environment.

In the next tutorial, we'll see how to read and write files using Node.js

developed & nourished with by rishabh.io