JavaScript Performance Of Recursion Vs. Loop

We will contrast the effectiveness of loops and recursion methods today.

Recursion is simply the practice of doing an action repeatedly. Similar to this, the Recursion strategy in JavaScript is employed to call the same function inside the method in order to obtain the result. It makes the algorithm easier to understand. Let’s find out whether it is performance-friendly or not.

Example

To determine the power of any number, I will create a computer program. To test the effectiveness of the algorithms, I will create them using both loops and recursion.

function powUsingLoop(x, n) {  
    var result = 1;  
    for (var i = 0; i < n; i++) {  
        result *= x;  
    }  
    return result;  
}  
  
function powUsingRecursion(x, n) {  
    if (n == 1) {  
        return x;  
    } else {  
        return x * powUsingRecursion(x, n - 1);  
    }  
}

Both of the functions I built take two parameters. I applied to loop notions in the first function, and I employed recursion techniques in the second. I’m going to call both of the functions right away.

Console will be used by me.

the time needed to compute execution time.

Let’s test it out in small quantities to see whether it functions flawlessly.

console.time("looping #1");  
console.log(powUsingLoop(2,3));  
console.timeEnd("looping #1");  
console.time("Recursion #1");  
console.log(powUsingRecursion(2,3));  
console.timeEnd("Recursion #1");

Output

8

looping #1: 8.000244140625ms
8
Recursion #1: 0.999755859375ms

According to this, 2 + 3 = 8. This is what both functions produced. However, the time difference is obvious. Recursion was completed in less than 1ms as opposed to looping’s 8ms.

Let’s raise the totals!

console.time("looping #2");  
console.log(powUsingLoop(2,1000));  
console.timeEnd("looping #2");  
console.time("Recursion #2");  
console.log(powUsingRecursion(2,1000));  
console.timeEnd("Recursion #2");

Output

1.0715086071862673e+301

looping #2: 11.000244140625ms
1.0715086071862673e+301
Recursion #2: 1ms

Recursion took just 1ms to finish the evaluation as opposed to the looping method’s 11ms.

Let’s raise the totals once more.

console.time("looping #3");  
console.log(powUsingLoop(2,10000));  
console.timeEnd("looping #3");  
console.time("Recursion #3");  
console.log(powUsingRecursion(2,10000));  
console.timeEnd("Recursion #3");

Output

Infinity

looping #3: 13ms

Uncaught RangeError: Maximum call stack size exceeded

Surprised!!

It was successfully done for looping, producing Infinity.

But it failed because of Recursion. This is due to the fact that when recursion is called, the call stack is eventually exceeded by the sum of all function calls. The maximum number of calls that can be present on a stack at any given moment is controlled by stack size or memory exhaustion depending on the browser.

Most of the time, recursion takes less time than looping. But the recursion functions should have a breaking condition in place to make sure they don’t go above the call stack limit and fail.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories