second-largest-element

Benchmark created by Carroll Yu on


Test runner

Ready to run.

Testing in
TestOps/sec
sorted
function secondLargestElement(input) {
  if (input instanceof Array) {
    input.sort(function(a, b) {
      return b - a
    });
    return input[1];
  } else {
    return 'Input is not an array.';
  }
};

var numbers = [37, 4279, 3426, 6178, 8882, 830, 9406, 3013, 1026, 3924, 284, 8346, 147, 7093, 2212, 2526, 2528, 1494, 3792, 4555, 9530, 4775, 1627, 2424, 637, 6769, 2681, 1245, 6946, 9497, 4186, 382, 2480, 8672, 5385, 7740, 2319, 8187, 8369, 7392, 3079, 8431, 6834, 1968, 2116, 6036, 3106, 8849, 1154, 9162, 4170, 6052, 7048, 9318, 6872, 517, 8027, 9558, 7686, 9592, 6810, 37, 4830, 4283, 4614, 704, 3451, 1783, 55, 1276, 7426, 9986, 670, 1121, 3330, 7559, 9229, 9937, 6337, 8692, 8948, 6261, 2495, 7042, 7179, 8198, 2118, 4431, 9175, 7578, 1177, 5773, 6096, 823, 2299, 2119, 7108, 7369, 5475, 1931, 8872, 6014, 4423, 236, 3283, 7324, 6056, 3045, 6174, 4856, 4880, 6023, 2190, 6183, 5484, 3026, 9698, 4279, 3426, 6178, 8882, 830, 9406, 3013, 1026, 3924, 284, 8346, 147, 7093, 2212, 2526, 2528, 1494, 3792, 4555, 9530, 4775, 1627, 2424, 637, 6769, 2681, 1245, 6946, 9497, 4186, 382, 2480, 8672, 5385, 7740, 2319, 8187, 8369, 7392, 3079, 8431, 6834, 1968, 2116, 6036, 3106, 8849, 1154, 9162, 4170, 6052, 7048, 9318, 6872, 517, 8027, 9558, 7686, 9592, 6810, 37, 4830, 4283, 4614, 704, 3451, 1783, 55, 1276, 7426, 9986, 670, 1121, 3330, 7559, 9229, 9937, 6337, 8692, 8948, 6261, 2495, 7042, 7179, 8198, 2118, 4431, 9175, 7578, 1177, 5773, 6096, 823, 2299, 2119, 7108, 7369, 5475, 1931, 8872, 6014, 4423, 236, 3283, 7324, 6056, 3045, 6174, 4856, 4880, 6023, 2190, 6183, 5484, 3026, 9698, 4279, 3426, 6178, 8882, 830, 9406, 3013, 1026, 3924, 284, 8346, 147, 7093, 2212, 2526, 2528, 1494, 3792, 4555, 9530, 4775, 1627, 2424, 637, 6769, 2681, 1245, 6946, 9497, 4186, 382, 2480, 8672, 5385, 7740, 2319, 8187, 8369, 7392, 3079, 8431, 6834, 1968, 2116, 6036, 3106, 8849, 1154, 9162, 4170, 6052, 7048, 9318, 6872, 517, 8027, 9558, 7686, 9592, 6810, 37, 4830, 4283, 4614, 704, 3451, 1783, 55, 1276, 7426, 9986, 670, 1121, 3330, 7559, 9229, 9937, 6337, 8692, 8948, 6261, 2495, 7042, 7179, 8198, 2118, 4431, 9175, 7578, 1177, 5773, 6096, 823, 2299, 2119, 7108, 7369, 5475, 1931, 8872, 6014, 4423, 236, 3283, 7324, 6056, 3045, 6174, 4856, 4880, 6023, 2190, 6183, 5484, 3026, 9698];

var res = secondLargestElement(numbers);
console.log(res);
ready
unsorted
var secondLargestElement = function(input) {
  var first = 0;
  var second = 0;
  if (input instanceof Array) {
    for (var i = 0; i < input.length; i++) {
      if (input[i] > first) {
        second = first;
        first = input[i];
      } else if (input[i] > second) {
        second = input[i];
      }
    }
  } else {
    return 'Input is not an array.';
  }
  return second;
};

var numbers = [37, 4279, 3426, 6178, 8882, 830, 9406, 3013, 1026, 3924, 284, 8346, 147, 7093, 2212, 2526, 2528, 1494, 3792, 4555, 9530, 4775, 1627, 2424, 637, 6769, 2681, 1245, 6946, 9497, 4186, 382, 2480, 8672, 5385, 7740, 2319, 8187, 8369, 7392, 3079, 8431, 6834, 1968, 2116, 6036, 3106, 8849, 1154, 9162, 4170, 6052, 7048, 9318, 6872, 517, 8027, 9558, 7686, 9592, 6810, 37, 4830, 4283, 4614, 704, 3451, 1783, 55, 1276, 7426, 9986, 670, 1121, 3330, 7559, 9229, 9937, 6337, 8692, 8948, 6261, 2495, 7042, 7179, 8198, 2118, 4431, 9175, 7578, 1177, 5773, 6096, 823, 2299, 2119, 7108, 7369, 5475, 1931, 8872, 6014, 4423, 236, 3283, 7324, 6056, 3045, 6174, 4856, 4880, 6023, 2190, 6183, 5484, 3026, 9698, 4279, 3426, 6178, 8882, 830, 9406, 3013, 1026, 3924, 284, 8346, 147, 7093, 2212, 2526, 2528, 1494, 3792, 4555, 9530, 4775, 1627, 2424, 637, 6769, 2681, 1245, 6946, 9497, 4186, 382, 2480, 8672, 5385, 7740, 2319, 8187, 8369, 7392, 3079, 8431, 6834, 1968, 2116, 6036, 3106, 8849, 1154, 9162, 4170, 6052, 7048, 9318, 6872, 517, 8027, 9558, 7686, 9592, 6810, 37, 4830, 4283, 4614, 704, 3451, 1783, 55, 1276, 7426, 9986, 670, 1121, 3330, 7559, 9229, 9937, 6337, 8692, 8948, 6261, 2495, 7042, 7179, 8198, 2118, 4431, 9175, 7578, 1177, 5773, 6096, 823, 2299, 2119, 7108, 7369, 5475, 1931, 8872, 6014, 4423, 236, 3283, 7324, 6056, 3045, 6174, 4856, 4880, 6023, 2190, 6183, 5484, 3026, 9698, 4279, 3426, 6178, 8882, 830, 9406, 3013, 1026, 3924, 284, 8346, 147, 7093, 2212, 2526, 2528, 1494, 3792, 4555, 9530, 4775, 1627, 2424, 637, 6769, 2681, 1245, 6946, 9497, 4186, 382, 2480, 8672, 5385, 7740, 2319, 8187, 8369, 7392, 3079, 8431, 6834, 1968, 2116, 6036, 3106, 8849, 1154, 9162, 4170, 6052, 7048, 9318, 6872, 517, 8027, 9558, 7686, 9592, 6810, 37, 4830, 4283, 4614, 704, 3451, 1783, 55, 1276, 7426, 9986, 670, 1121, 3330, 7559, 9229, 9937, 6337, 8692, 8948, 6261, 2495, 7042, 7179, 8198, 2118, 4431, 9175, 7578, 1177, 5773, 6096, 823, 2299, 2119, 7108, 7369, 5475, 1931, 8872, 6014, 4423, 236, 3283, 7324, 6056, 3045, 6174, 4856, 4880, 6023, 2190, 6183, 5484, 3026, 9698];

var res = secondLargestElement(numbers);
console.log(res);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Carroll Yu on