How to solve the issue that arise while using performance.now() method in javaScript?

Any coder, given enough time, can solve a problem, the only thing matters are. Performance plays a very important role in software. A website with good performance can attract more number of users whereas the one with comparatively lesser or poor performance is not able to do so. As the use of JavaScript is increasing day by day in making very large website projects, it is important to make sure our code is fast. But how can one make sure that the code is fast enough! Generally, the best way to check the performance of the functions in JavaScript is the built-in performance.now() method.

To measure the performance of a function, i.e. how much time does a function takes to execute or to calculate running time,performance.now() method is widely been used. It returns the time elapsed since the origin of the program. The concept would be more clear by having a look at the following problem statement.

Exmaple: We will see how much time will it take to traverse an array of 100000 elements.

  • Program:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
    /* Creating a massive array of 100000 elements*/
    const array2 = new Array(100000).fill('Gfg');
    const { performance } = require('perf_hooks');
      
    /*Calculating the time time taken */
    function calculatePerformance(array) {
        /*Before beginning the loop, calculating performance*/
        let t0 = performance.now();
        console.log(array)
        for (let i = 0; i < array.length; i++) {
            if (array[i] == 'Gfg') {
                console.log("Gfg is present in array");
            }
        }
        //Calculating performance after the loop is executed
        let t1 = performance.now();
      
        console.log("call to the above function took" 
        + (t1 - t0) + "milliseconds");
    }
    calculatePerformance(array2); 
    </script>

    chevron_right

    
    

  • Output:
    call to the above function took1385.733816999942milliseconds

In the above example, it can clearly be seen how we can check how fast the function is using performance.now(). Further, this method returns a floating-point number that reflects the current time in milliseconds (accurate to a thousandth of a millisecond). This is a very simple technique to measure the performance of a function. This time is not fixed, it may change every time we run the code again. As the number of elements in the array increases, the function becomes slower and the runtime increases. If we run the same program on different computers can take different time, the reason being the CPU. The time taken to run a certain function or to perform a certain task depends on how powerful the CPU is, and what other programs are running on a particular computer.

Issue with performance.now(): Here, a problem here will arise, because if two persons have written exactly the same code, we cannot conclude that the user one whose runtime was less has written a better code. Here comes the need of Big O notation.



Big-O notation: Big O is use to measure the performance of different functions. There can be different solutions to the same problem, but among those, which one is better and more efficient. This is what Big O tells. Big O doesn’t measure things in seconds. Instead, it focuses on how quickly the runtime grows on increasing the number of operations. It provides a standard method to calculate the performance of the functions.

Thus, for the above example, even though the run time may vary a little bit from person to person, but the Big O calculated for the same program will be the same for each and every person. Since an algorithm’s running time may vary among different inputs of the same size. We use Big -O to calculate the worst-case time complexity, which is the maximum amount of time required for inputs of a given size. This is the main reason why we calculate Big O of different functions.

Taking a simple example of solving the rubik’s-cube, in how much time it will be solved, it depends on person to person, one can solve it in just 30 seconds and the other may take even hours to solve that. We can define the worst case, which is the maximum amount of time taken by a person to solve the cube. The same is the case where we’re talking about, Big-O tells the worst time and we can estimate the performance of a function in a standard way. For the same problem statement, if we calculate the Big-O, it will be O(n). This means that the running time will increase linearly with the size of the input, more the number of operations in the input, more will be the time taken by the function. Big-O is a more precise way to calculate the performance because it tells the worst case, that is the maximum number of time the function will take to execute.

Solution by Big O notation: By having a look at the given function, we can see that the size of the array doesn’t matter in calculating the runtime of function. The function will run in O(1) time also known as constant time. No matter whether the input array consists of 1 item, 100 items, or 1000 items, this function would still require just one step.

  1. O(1)
  2. O(1)
  3. O(1)

Thus, it can be concluded that Big-O is a more precise way to calculate the performance because it tells the worst case, i.e. the maximum amount of time the function will take to execute, and moreover it gives a standard solution which is applicable to all the other functions of the same type.

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.