Author -  Sai gowtham

How to Measure the Function Execution time in JavaScript

In this tutorial, we are going to learn about measuring the function execution time in JavaScript.

Consider we have an add() function which is used to add the two numbers and returns their sum.

function add(a,b){
    return a+b;
}

console.log(add(10,10)); //20

Now we need to measure, how much time it takes to execute this add() function by using performance.now() method.

Measuring function execution time

The performance.now() method returns a high-resolution timestamp in milliseconds.

Example:

let t0= performance.now(); //start time
add(10,10); //the function we need to measure
let t1= performance.now(); //end time

console.log('Time taken to execute add function:'+ (t1-t0) +' milliseconds');

In the above code, we have invoked add() function in between t0(start time) and t1(end time) ,inside console.log() we subtracted t1-t0 so that we can get the add() function execution time in milliseconds.

add-function-execution-time

If you want to measure the time in seconds instead of milliseconds we can do that by dividing milliseconds with 1000.

console.log('Time taken to execute add function:'+ (t1-t0)/1000 +' seconds');

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY