How to get the index of the function in an array of functions which executed the fastest in JavaScript ?
Last Updated :
30 Dec, 2022
In this example, we will learn how to get the index of the function in an array of functions that is executed the fastest in JavaScript.
Example:
Input: fun[] = [ hello, hello1, hello2 ]
Output: index of fastest is 0.
Explanation: Function hello execute fastest in all functions.
Input: fun[] = [ while1, while2, while3 ]
Output: index of fastest function is 2
Approach: The below steps have to be followed to solve the problem:
- We will first Iterate over the given array.
- We will find the time taken by each function and store it in a different array with the same index value as the function index. The time taken can be found by getting the difference in time using the performance.now() method.
- Finally, we print the minimum index by getting the minimum value of the array using the Math.min() method.
Example 1: In this example, we will calculate the time taken by each function to execute. We will then print the index of the fastest function. using the above-mentioned approach
Javascript
<script>
function hello() {
var s = "" ;
var ans = [ "The" , " hello function " , "takes " ];
for ( var i = 0; i < 3; i++) s += ans[i];
console.log(s);
}
function hello1() {
var s = "" ;
var ans = [ "The hello1 function" , " takes " ];
for ( var i = 0; i < 2; i++) s += ans[i];
console.log(s);
}
function hello2() {
var ans = "The hello2 function takes " ;
for ( var i = 0; i < 1; i++) console.log(ans);
}
function findTime(f) {
var start = performance.now();
f();
var end = performance.now();
return end - start;
}
function findMinTime() {
var fun = [hello, hello1, hello2];
var ans = [];
for ( var i = 0; i < 3; i++) {
var n = findTime(fun[i]);
ans[i] = n;
console.log(ans[i]);
}
var answer = Math.min.apply( null , ans);
c = ans.indexOf(answer);
return c;
}
var minTime = findMinTime();
console.log( "Index of fastest function:" , minTime);
</script>
|
Output:
"The hello function takes "
0.10000000009313226
"The hello1 function takes "
0
"The hello2 function takes "
0
"Index of fastest function:"
1
Example 2: In this example, we will calculate the time taken by functions to perform some mathematical operations. We will then print the index of the fastest function.
Javascript
<script>
function fac(n) {
let fact = 1;
for (let i = 1; i <= 4; i++)
fact *= i;
console.log( "Factorial of 4 is:" , fact);
}
function fibo() {
let fab = 0;
let j = 1;
for (let i = 2; i <= 6; i++) {
let temp = fab;
fab += j;
j = temp;
}
console.log( "6th fibonacci no is:" , fab);
}
function binpow() {
let j = 2;
let k = 22;
for (let i = 0; i < k; i++)
j = ((j * j) % 1e9) + 7;
console.log(
"Power 2 to 22 mod 1e9+7 is:" , j
);
}
function findTime(f) {
var start = performance.now();
f();
var end = performance.now();
return end - start;
}
function findMinTime() {
var fun = [fac, fibo, binpow];
var ans = [];
for ( var i = 0; i < 3; i++) {
var n = findTime(fun[i]);
ans[i] = n;
console.log(ans[i]);
}
var answer = Math.min.apply( null , ans);
c = ans.indexOf(answer);
return c;
}
var minTime = findMinTime();
console.log( "Index of fastest function:" ,
minTime);
</script>
|
Output:
Factorial of 4 is: 24
0.30000001192092896
6th fibonacci no is: 5
0.20000001788139343
Power 2 to 22 mod 1e9+7 is: 221047735
0.30000001192092896
Index of fastest function: 1
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...