Jump to content

How Can We Calculate The Elapsed Time For A Function To Be Executed?


Eyad Syria-lover
 Share

Recommended Posts

If you care about compatibility with older browsers, you can use Date() objects. It has millisecond precision, but you can benchmark code by running it thousands of times.

var iterations = 100000;

var start = (new Date()).getTime();
for(var i = 0; i < iterations; i++) {
  // Put code here for benchmarking
}
var end = (new Date()).getTime();

// Find out how long it took
var elapsedTime = end - start;

// Find out how much time passes for just one function call
var averageTime =  elapsedTime / iterations;

// Output data
console.log(averageTime);
Link to comment
Share on other sites

If you care about compatibility with older browsers, you can use Date() objects. It has millisecond precision, but you can benchmark code by running it thousands of times.

var iterations = 100000;

var start = (new Date()).getTime();
for(var i = 0; i < iterations; i++) {
  // Put code here for benchmarking
}
var end = (new Date()).getTime();

// Find out how long it took
var elapsedTime = end - start;

// Find out how much time passes for just one function call
var averageTime =  elapsedTime / iterations;

// Output data
console.log(averageTime);

This Is Taking More Than Expected Because The Iteration Is Very Big,Do You Have Another Way Sir?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...