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>
// 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
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read