How to get the index of the function in an array of functions which executed the fastest in JavaScript ? Last Updated : 30 Dec, 2022 Comments Improve Suggest changes Like Article Like Report 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> // 1st function function hello() { var s = ""; var ans = ["The", " hello function ", "takes "]; for (var i = 0; i < 3; i++) s += ans[i]; console.log(s); } // 2nd function function hello1() { var s = ""; var ans = ["The hello1 function", " takes "]; for (var i = 0; i < 2; i++) s += ans[i]; console.log(s); } // 3rd function function hello2() { var ans = "The hello2 function takes "; for (var i = 0; i < 1; i++) console.log(ans); } // Function to check time required by each function function findTime(f) { // Storing initial time in start var start = performance.now(); // Calling the function f(); // Storing time after running the function var end = performance.now(); // Return time taken by function return end - start; } function findMinTime() { // Initializing array of functions var fun = [hello, hello1, hello2]; // Initialising array of time taken by function var ans = []; // Iterating over all the functions and // storing time taken by them for (var i = 0; i < 3; i++) { var n = findTime(fun[i]); ans[i] = n; console.log(ans[i]); } // Finding the minimum time in array var answer = Math.min.apply(null, ans); c = ans.indexOf(answer); // Return index of fastest array 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> // 1st function function fac(n) { let fact = 1; for (let i = 1; i <= 4; i++) fact *= i; console.log("Factorial of 4 is:", fact); } // 2nd function 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); } // 3rd function 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 to check time required // by each function function findTime(f) { // Storing initial time in start var start = performance.now(); // Calling the function f(); // Storing time after running the function var end = performance.now(); // Return time taken by function return end - start; } function findMinTime() { // Initializing array of functions var fun = [fac, fibo, binpow]; // Initialising array of time // taken by function var ans = []; // Iterating over all the functions // and storing time taken by them for (var i = 0; i < 3; i++) { var n = findTime(fun[i]); ans[i] = n; console.log(ans[i]); } // Finding the minimum time in array var answer = Math.min.apply(null, ans); c = ans.indexOf(answer); // Return index of fastest array 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 Comment More infoAdvertise with us Next Article How to get the index of the function in an array of functions which executed the fastest in JavaScript ? S satyam00so Follow Improve Article Tags : JavaScript Web Technologies javascript-functions JavaScript-Questions Similar Reads How to get index at which the value should be inserted into sorted array based on iterator function in JavaScript ? In this article, we will learn how to get an index at which the value should be inserted into a sorted array based on the iterator function in JavaScript. Here we use the sorting function to sort the array and we would print the array and the position of the new element in the DOM.Approach:Create an 4 min read How to store JavaScript functions in a queue and execute in that order? In this article, the task is to execute the functions in the order defined in the queue with the help of JavaScript. There are two approaches that are discussed below. Approach 1: Declare the functions and use the array push() method to push the functions in the array. Later traverse the array and e 2 min read How to create a function that invokes each provided function with the arguments it receives using JavaScript ? In this article, we will see how to create a function that invokes each provided function with the arguments it receives using JavaScript. It helps in reducing the effort of creating the same instance of code again and again. In this article let us see how to create a function that invokes each prov 3 min read How to Get Index of Greatest Value in an Array in JavaScript ? In this article, we will see how to return the index for the greatest value in an array with the help of JavaScript. To return the index of the greatest value in an array using JavaScript.Some of the examples are to return the index of the greatest value in an array.Example: The greatest value in th 4 min read How to get the function name from within that function using JavaScript ? Given a function and the task is to get the name of the function from inside the function using JavaScript. There are basically two methods to get the function name from within that function in JavaScript. These are: Using String substr() MethodUsing Function prototype name propertyGet the Function 2 min read How to get removed elements of a given array until the passed function returns true in JavaScript ? The arrays in JavaScript have many methods which make many operations easy. In this article let us see different ways how to get the removed elements before the passed function returns something. Let us take a sorted array and the task is to remove all the elements less than the limiter value passed 4 min read How to get index of object inside an array that matches the condition in jQuery ? jQuery is a free and open-source JavaScript library designed to add interactivity to the HTML web pages. jQuery is similar to JavaScript in terms of functionality but what makes it popular is its simplicity and ease of use. jQuery comes bundles with inbuilt methods that help achieve the desired outp 4 min read What is the fastest way to loop through an array in JavaScript ? The fastest way to loop through an array in JavaScript depends upon the usecases and requirements. JavaScript has a large variety of loops available for performing iterations. The following loops along with their syntax are supported by JavaScript. Table of Content for loop while loop .forEach() loo 3 min read How to get an array of function property names from own enumerable properties of an object in JavaScript ? Enumerable Properties: All the properties of an object that can be iterated using for..in loop or Object.keys() method are known as enumerable properties. In this article, we will see how to get an array of functions of an object that are enumerable. It can be achieved by following these 2 steps. Us 2 min read How to Find the Index of an Element that Contains the Given Substring in Array ? To find the index of an element within an array that contains a specific substring in JavaScript, there are multiple strategies available. The methods offer flexibility in handling various scenarios, allowing you to efficiently locate the index of the desired substring within the array. Table of Con 2 min read Like