5

Assume we have some array and I want to get the maximum of the elements.

array.sort((a, b) => b - a)[0]

and

array.reduce((a, b) => (a > b) ? a : b)

will both return the maximum.

The question is, which one is preferred?

For a specific case of my project, I could be iterating over 100+ elements, 100+ times which I thought would be big enough to matter for the small performance difference.

7
  • I think array.reduce is faster. Sorting always needs more operations behind Commented Nov 27, 2017 at 11:14
  • 4
    I would prefer Math.max(...arr) myself Commented Nov 27, 2017 at 11:14
  • 2
    reduce is O(n), sort is O(nlogn). Commented Nov 27, 2017 at 11:14
  • 1
    @KarlReid Math.max is not recommended for large array. Commented Nov 27, 2017 at 11:21
  • 1
    @NinaScholz I didn't know that. Sounds like reduce is the winner then. :D Commented Nov 27, 2017 at 11:22

2 Answers 2

7

array.reduce seems faster..

var numbers = [];


function myFunction(item) {
 for (var i = 0 ; i<100000 ; i++)
 {
   numbers.push(i); 
 }


 var t0 = performance.now();
 var x = numbers.reduce((a, b) => (a > b) ? a : b);
 var t1 = performance.now()
 document.getElementById("timex").innerHTML = (t1 - t0) + " milliseconds";
 
 var t2 = performance.now();
 var y = numbers.sort((a, b) => b - a)[0];
 var t3 = performance.now();
 
 document.getElementById("timey").innerHTML = (t3 - t2) + " milliseconds";
 document.getElementById("reduce").innerHTML = x
 document.getElementById("sort").innerHTML = y
}
<html>
<body>

<p>Click the button to get the highest numbers in the array.</p>
<button onclick="myFunction()">Try it</button>

<p>Highest number (array.reduce): <span id="reduce"></span>  time taken : <span id="timex"></span></p>
<p>Highest number(array.sort): <span id="sort"></span> time taken:<span id="timey"></p>


<script>

</script>

</body>
</html>

Sign up to request clarification or add additional context in comments.

5 Comments

Pointers, its always better to use jsPerf for performance related stuff. They loop automatically based on few criteria to even test cached values. Second, instead of for(...) { numbers.push(i) }, you can try var numbers = Array.from({length: 100000}. (x,i) => i), if you wish to use ES6 methods
@Rajesh sure, I will look into it. It is good to learn a new thing every day.
@priyadarshiswain Glad I could help. You can refer jsperf comparison and/or add in your answer for readers.
Also I just realized, var numbers = [10000]; is not doing anything. Here 10000 will be replaced in loop. So you can ignore this part. Just set it to blank array.
@Rajesh I used that to check if the logic was indeed returning largest number for a lesser loop count. I Should have removed it in the final code.
1

I tried to check performance and this is result, hope you know more about them.

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.