minIndex vs Math min index

Edit on GitHub

Setup

var arr = [91142, 61826, 23563, 55121, 14611, 74745, 33967, 80034, 42361, 75684, 16252, 35423, 45911, 61087, 68761, 18844, 51965, 60062, 14560, 2721, 11899, 19598, 49904, 48040, 72684, 67244, 82390, 98910, 6856, 75994, 72128, 7317, 14505, 22428, 90388, 74943, 12781, 92686, 532, 91531, 22361, 74512, 97687, 86907, 39873, 26170, 2908, 35369, 85074, 58231, 82162, 1995, 16191, 11888, 42010, 66163, 69380, 97681, 64002, 31660, 69591, 36371, 86874, 66724, 4846, 26199, 73133, 59958, 19477, 44949, 70420, 73229, 38444, 29924, 38430, 29545, 29485, 69289, 77170, 37406, 26318, 24032, 56903, 92924, 87202, 61766, 11245, 8879, 96066, 68516, 34620, 24602, 12212, 26335, 59620, 5243, 28277, 6426, 4175, 68481];

Tests

  • minIndex

    function minIndex(arr) {
        if (arr.length === 0) return -1;
    
        let min = arr[0];
        let index = 0;
    
        for (let i = 1; i < arr.length; i++) {
            if (arr[i] < min) {
                index = i;
                min = arr[i];
            }
        }
        return index;
    }
    
    var index =  minIndex(arr);
    
    console.log(index);
  • Math min index

    var index = arr.indexOf(Math.min.apply(Math, arr));
    console.log(index);