title: Javascript performance: reduce() vs for-loop
body: <p>I was attempting <a href="https://www.codewars.com/kata/integers-recreation-one/train/javascript" rel="noreferrer">this Codewars challenge</a> and the problem involves finding the divisors of a number and then calculating the sum of these divisors squared. I found two approaches to this problem.</p>
  
  <p>The first approach is based on another Stackoverflow questions about <a href="https://stackoverflow.com/questions/43150520/javascript-find-the-sum-of-all-divisors-of-a-given-integer">finding the sum of all divisors</a> and seems clever at first:</p>
  
  <pre><code>function divisorsSquared(n) {
    // create a numeric sequence and then reduce it
    return [...Array(n+1).keys()].slice(1)
     .reduce((sum, num)=&gt;sum+(!(n % (num)) &amp;&amp; Math.pow(num,2)), 0);
  }
  </code></pre>
  
  <p>The second approach I used was using a simple for-loop:</p>
  
  <pre><code>function divisorsSquared(n) {
    var sum = 0;
    for(var i = 1; i&lt;= n; i++){
       if(n % i === 0) sum += Math.pow(i,2); 
    }
    return sum;
  }
  </code></pre>
  
  <p>Now I noticed that the first approach is significantly slower than the second and a quick <a href="https://jsperf.com/squared-divisors-sum-reduce-vs-for-loop/1" rel="noreferrer">jsperf test</a> confirms this.</p>
  
  <p>My questions are: Why is the first approach so much slower and what approach is preferable in production code?</p>
  
  <p>On Codewars I notice that for many challenges there are clever one-line solutions using similar array methods. As a beginner, may such solutions be considered better practice than for-loops, even if performance is worse?</p>