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

## Recommended Posts

Is There Any Other Way To Solve This?.

##### 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);```
##### Share on other sites

But I Felt Uncomfortable About It.

What does that mean? What about it makes you feel uncomfortable?
##### Share on other sites

What does that mean? What about it makes you feel uncomfortable?

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

Like This:

10.925054550170898

##### 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?

##### Share on other sites

Reduce the number of iterations.

##### 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.