Comparing the performance of recursive and looped factorial function in JavaScript

We will be writing two JavaScript functions, the job of both the functions will be to take in a number and return its factorial.

The first function should make use of a for loop or while loop to compute the factorial. Whereas the second function should compute the factorial using a recursive approach.

Lastly, we should compare the times taken by these functions over a large number of iterations.

Iterative Factorial Function

The iterative approach uses a simple for loop to calculate the factorial:

const factorial = (num = 1) => {
    let result = 1;
    for (let i = 2; i <= num; i += 1) {
        result *= i;
    }
    return result;
}

// Test the function
console.log(factorial(5));  // 120
console.log(factorial(7));  // 5040
120
5040

Recursive Factorial Function

The recursive approach calls itself with a decremented value until it reaches the base case:

const factorialRecursive = (num = 1) => {
    if(num > 1){
        return num * factorialRecursive(num - 1);
    }else{
        return 1;
    }
};

// Test the function
console.log(factorialRecursive(5));  // 120
console.log(factorialRecursive(7));  // 5040
120
5040

Performance Comparison

Let's compare the performance by running both functions multiple times and measuring the execution time:

const factorial = (num = 1) => {
    let result = 1;
    for (let i = 2; i <= num; i += 1) {
        result *= i;
    }
    return result;
}

const factorialRecursive = (num = 1) => {
    if(num > 1){
        return num * factorialRecursive(num - 1);
    }else{
        return 1;
    }
};

const ITERATIONS = 100000000;
const num = 12;

console.time('Looping Approach');
for(let i = 0; i < ITERATIONS; i++){
    factorial(num);
};
console.timeEnd('Looping Approach');

console.time('Recursive Approach');
for(let j = 0; j < ITERATIONS; j++){
    factorialRecursive(num);
};
console.timeEnd('Recursive Approach');
Looping Approach: 886.720ms
Recursive Approach: 6526.203ms

Performance Analysis

The results clearly show that the iterative approach is significantly faster than the recursive approach. This performance difference occurs because:

  • Function call overhead: Recursive calls create new stack frames for each function call
  • Memory usage: Recursion uses more memory due to maintaining the call stack
  • CPU efficiency: Loops are more CPU-efficient than repeated function calls
Approach Time Complexity Space Complexity Performance
Iterative O(n) O(1) Faster
Recursive O(n) O(n) Slower

Conclusion

While recursive solutions are often more elegant and easier to understand, iterative approaches generally provide better performance for factorial calculations. The execution time ratio may vary between machines, but iterative solutions will consistently outperform recursive ones for this type of computation.

Updated on: 2026-03-15T23:19:00+05:30

266 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements