Created
August 17, 2016 22:30
-
-
Save jiggzson/f41fcbd5a0bc385d1211bd8d6f3678df to your computer and use it in GitHub Desktop.
Find smallest number in Javascript array. Much faster than Math.min.apply
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function min(arr) { | |
var l, a, b; | |
while(true) { | |
l = arr.length; | |
if(l < 2) return arr[0]; | |
a = arr.pop(); | |
b = arr[l-2]; | |
if(a < b) { | |
arr.pop(); | |
arr.push(a); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment