Jump to content

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


Eyad Syria-lover

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

It's Returning So Many Numbers After Decimal...

So, what's the problem? Most floating-point numbers in Javascript aren't going to be precise after a few decimals, so if the precision of that is supposed to be to 3 decimal places then you can drop the other digits.
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
×
×
  • Create New...