Array sum: Comparing recursion vs for loop vs ES6 methods in JavaScript

When working with arrays in JavaScript, there are multiple ways to calculate the sum of all elements. Let's compare three popular approaches: recursion, traditional for loops, and ES6 methods like reduce().

To demonstrate performance differences, we'll test each method with a large number of iterations. This gives us insights into which approach performs best for array summation tasks.

Recursive Approach

The recursive method calls itself until it processes all array elements:

const recursiveSum = (arr, len = 0, sum = 0) => {
    if(len < arr.length){
        return recursiveSum(arr, len+1, sum + arr[len]);
    };
    return sum;
};

For Loop Approach

The traditional loop iterates through each element and accumulates the sum:

const loopingSum = arr => {
    let sum = 0;
    for(let i = 0; i < arr.length; i++){
        sum += arr[i];
    };
    return sum;
};

ES6 Reduce Approach

The modern ES6 reduce() method provides a functional programming approach:

const ES6Sum = arr => arr.reduce((acc, val) => acc + val);

Performance Comparison Example

Let's test all three methods with a large number of iterations to measure performance:

const ITERATIONS = 1000000;  // Reduced for demo
const arr = [12, 65, 87, 2, 23, 87, 4, 66, 34, 89, 89, 32, 4];

const recursiveSum = (arr, len = 0, sum = 0) => {
    if(len < arr.length){
        return recursiveSum(arr, len+1, sum + arr[len]);
    };
    return sum;
};

const loopingSum = arr => {
    let sum = 0;
    for(let i = 0; i < arr.length; i++){
        sum += arr[i];
    };
    return sum;
};

const ES6Sum = arr => arr.reduce((acc, val) => acc + val);

// Test recursive approach
console.time('recursive approach');
for(let k = 0; k < ITERATIONS; k++){
    recursiveSum(arr);
};
console.timeEnd('recursive approach');

// Test looping approach
console.time('looping approach');
for(let l = 0; l < ITERATIONS; l++){
    loopingSum(arr);
};
console.timeEnd('looping approach');

// Test ES6 approach
console.time('ES6 approach');
for(let m = 0; m < ITERATIONS; m++){
    ES6Sum(arr);
};
console.timeEnd('ES6 approach');
recursive approach: 2.847s
looping approach: 0.628s
ES6 approach: 0.498s

Simple Usage Examples

Here's how each method works with a basic array:

const numbers = [1, 2, 3, 4, 5];

console.log("Recursive sum:", recursiveSum(numbers));
console.log("Loop sum:", loopingSum(numbers));
console.log("ES6 sum:", ES6Sum(numbers));
Recursive sum: 15
Loop sum: 15
ES6 sum: 15

Performance Analysis

Method Readability Performance Memory Usage
Recursive Good Slowest High (stack frames)
For Loop Good Fast Low
ES6 Reduce Excellent Fastest Low

Key Points

  • ES6 reduce() offers the best performance and cleanest syntax
  • For loops provide good performance with familiar syntax
  • Recursion is slower due to function call overhead and stack usage
  • Performance results may vary between JavaScript engines and systems

Note: For accurate performance testing, avoid using online IDEs as they may have limitations that affect timing measurements.

Conclusion

For array summation tasks, ES6's reduce() method provides the best combination of performance, readability, and conciseness. While recursion is elegant, it's less efficient for large datasets due to function call overhead.

Updated on: 2026-03-15T23:18:59+05:30

330 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements