How to get the index of the fastest function in an array of functions using JavaScript?

JavascriptProgramming ScriptsWeb DevelopmentFront End Technology

JavaScript for beginners

Best Seller

74 Lectures 10 hours

Modern Javascript for Beginners + Javascript Projects

Most Popular

112 Lectures 15 hours

The Complete Full-Stack JavaScript Course!

Best Seller

96 Lectures 24 hours

In this tutorial, we will be learning how to execute function and calculate time taken by the function to execute and we will print the index of that function which will take less time to execute.

Before moving to our problem let’s get to know about procedure to calculate execution time of a function.

We use performance.now() method to check the performance of the code and we can check the execution time of our current executing function using this method, performance.now() method returns calculated time in milliseconds. So we store starting time before function calling and again we calculate the time after function execution has been finished.

Example

The below example demonstrate how to compute the time taken by a function to be executed in JavaScript.

<html>
<title>Get standard deviation of an array of numbers using JavaScript. </title>
<body>
<script>
function dummy() {
var a = 0,
b = 0,
res = 0;

for (var i = 0; i < 1100; i++) {
b = a;
a = b + i;
res = a + b;
}
//console.log(res)
}

function fastestFunction() {
var startTime = performance.now()
dummy();
var endTime = performance.now()
var totalTime = (endTime - startTime);
document.write("Time taken by the function: ", totalTime);
}
fastestFunction()
</script>
</body>
</html>


Note − You may get different result in the output.

Now let’s come to our main problem to get the index of fastest function.

Steps

We will follow below steps to index of the function in an array of functions which executed the fastest in JavaScript −

Step 1 − Store the function names in the array.

Step 2 − Traverse through the whole array.

Step 3 − Calculate the starting time before calling the function.

Step 4 − Call the current function.

Step 5 − Calculate the ending time after execution of the function.

Step 6 − Calculate the difference of ending time ad starting time of the function.

Example

In the below example, we get the index of the function in an array of functions which executed the fastest in JavaScript.

<html>
<title>Index of function which executed fastest in JavaScript.</title>
<body>
<p> Time taken by four functions: <br> </p>
<script>
function first() {
let fa = 1;
for (let i = 1; i < 1212124; i++)
fa *= i;
//console.log(fa);
}

function second() {
var a = 0,
b = 0,
res = 0;

for (var i = 0; i < 1100; i++) {
b = a;
a = b + i;
res = a + b;
}
//console.log(res)
}

function third() {
var a = 1;
for (var i = 0; i < 4550; i++) {
a *= i;
}
//console.log(a);
}

function fourth() {
var i = 0;
while (i < 53300) {
i += 10;
}
//console.log(i*100);
}

function fastestFunction() {
var arr = [first, second, third, fourth];
var TimeTaken_byFunction = [];

for (var i = 0; i < 4; i++) {
var startTime = performance.now()
arr[i]();
var endTime = performance.now()
var totalTime = (endTime - startTime);
console.log(totalTime)
document.write(totalTime + "<br>")
TimeTaken_byFunction.push(totalTime);
}

var min = Math.min.apply(null, TimeTaken_byFunction);
var index = TimeTaken_byFunction.indexOf(min);
document.write("<br>Function which executes faster is at index: ", index);
}
fastestFunction()
</script>
</body>
</html>


The output we get here is total time taken by all the four functions and we store the index in the TimeTaken_byFunction array and here the fastest time is the minimum time.

So, this was the procedure to check and get the index of the function which executes faster.

Updated on 11-Oct-2022 08:34:37