The below example adds up 1 million numbers and gives the output. To get the sum of numbers it would definitely take some time by this way we could see the execution time properly.
var i, output=0; for (i = 1; i <= 1000000; i++) output += i;
If we need to perform this in the old traditional way then we could use JS date time and complete the task. Here is the example to measure the execution time using Date() and getTime() method.
var i, output=0 ; var startTime=new Date().getTime(); for (i = 1; i <= 1000000; i++) output += i; var endTime=new Date().getTime(); console.log(endTime-startTime); //Time Taken : 2010ms
var i, output=0; console.time('Addition of Million Numbers'); for (i = 1; i <= 1000000; i++) output += i; console.timeEnd('Addition of Million Numbers');
If you notice the above code it is much simpler now. All you need to do is to start the timer by using console.time() and it is ended by using console.timeEnd(). The timer name passed to both the functions must match in order for measuring to work properly.
Note that console.time() and console.timeEnd() are only supported by modern browsers, starting with Chrome 2, Firefox 10, Safari 4, and Internet Explorer 11