Skip to content

Instantly share code, notes, and snippets.

@jiggzson
Created August 17, 2016 22:30
Show Gist options
  • Save jiggzson/f41fcbd5a0bc385d1211bd8d6f3678df to your computer and use it in GitHub Desktop.
Save jiggzson/f41fcbd5a0bc385d1211bd8d6f3678df to your computer and use it in GitHub Desktop.
Find smallest number in Javascript array. Much faster than Math.min.apply
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