LongImp

Benchmark created by Greg Foley on


Description

Performance of the solutions.

Setup

var numbers = [ 10, 20, 30, 10, 20, 20, 30, 10, 20, 20, 30, 10, 20, 20, 30, 10, 20, 20, 30, 10, 20, 20, 30, 10, 20, 20, 30, 10, 20, 20, 30, 10, 20, 90, 30, 60, 20, 10, 20, 90, 30, 60, 20, 10, 20, 90, 30, 60, 20, 10, 20, 90, 30, 60, 20, 10, 20, 90, 30, 60, 20, 10, 20, 90, 30, 60, 20, 10, 20, 90, 30, 60, 20, 10, 20, 90, 30, 60, 20, 10, 20, 90, 30, 60, 20, 10, 20, 90, 30, 60, 20, 10, 20, 90, 30, 60, 20 ];

Test runner

Ready to run.

Testing in
TestOps/sec
Aezed
function longestImprovement (numbers) {
  var longest = 1;
  var count = 1;

  numbers.forEach(function(num, i) {
    num < numbers[i + 1] ? count++ : count = 1;
    if (count > longest) longest = count;
  });
  return longest;
}
ready
Frances
function longestImprovement (numbers) {
var longest = 1;
var result = 1;
  numbers.reduce(function(prev, next){
    if (next > prev) {
      result ++;
      longest = Math.max(longest, result);
    } else {
      result = 1;
    }
    return next;
  })
  return longest;
}
ready
Gabriel
function longestImprovement (numbers) {
  var countsArray = [];
  var counter = 1;
  numbers.forEach(function(element, index, array){
        if(array[index] > array[index-1]){
      counter++;
        } else {
          countsArray.push(counter);
          counter = 1;
        }
        return counter;
  });
  return Math.max.apply(null, countsArray);
};
ready
Greg
function longestImprovement(numbers) {

  var longestIncreasingSequence = 1;
  var thisIncreasingSequence = 1;
  
  var incrementIfIncrease = function(accumulator, currentValue) {
    if (currentValue > accumulator) {
      thisIncreasingSequence++;
      longestIncreasingSequence = Math.max(longestIncreasingSequence, thisIncreasingSequence);
    } else {
      thisIncreasingSequence = 1;
    }
    return currentValue;
  };
  
  numbers.reduce(incrementIfIncrease);
  
  return longestIncreasingSequence;
}
ready
Jason
function longestImprovement (numbers) {
  var result = 1; 
  var totals = [1];
  numbers.forEach(function(elem,i){
    if(numbers[i]>numbers[i+1]){
      totals.push(result);
      result=0;
    }
    result++;
  });
  return Math.max.apply(null, totals);
}
ready
Joe
function longestImprovement (numbers) {
  
  var count = 1;
  var status = 1;
    numbers.reduce(function(prev, item){
      if (item > prev) {
        count++;} else {
          if (count > status){
            status = count;
          }
      count = 1;
      }
      return item;
    }); 
  return status;
}
ready
Joseph
function longestImprovement (numbers) {
  // Write your code here, and
  // return your final answer.
  var maxSequence = 1;
  var thisSequence = 1;
  for(var i=0; i<numbers.length; i++){
    thisSequence = numbers[i+1] > numbers[i] ? thisSequence+=1 : 1;
    maxSequence = thisSequence > maxSequence ? thisSequence : maxSequence;
  }
  return maxSequence;
}
ready
Kent
function longestImprovement(numbers) {
  var counter = 0, temp = numbers[0], temp2 = 1;
  var holder = {
    0: 1
  };
  if (numbers.length === 1) {
    return temp2;
  }
  for (var i = 1; i < numbers.length; i++) {
    if (numbers[i] > temp) {
      holder[counter]++;
    }
    else {
      counter++;
      holder[counter] = 1;
      numbers[i] = temp;
    }
  }
  for (var key in holder) {
    if (holder[key] > temp2) {
      temp2 = holder[key];
    }
  }
  return temp2;
}
ready
Laura
function longestImprovement (numbers) {
  var counter = 0;
  var results = [];
  
  if(numbers.length < 2){
    return numbers.length;
  }
  
  numbers.reduce(function(initialVal, nextNum){
   if (nextNum > initialVal){
     counter ++;
     return initialVal = nextNum;
   } else {
     results.push(counter);
     counter = 1;
     return initialVal = 0;
   }
  }, 0); 
  console.log(results);
  return results.reduce(function(currentVal, nextVal){
    return currentVal > nextVal ? currentVal : nextVal;
  });
}
ready
Michael S
function longestImprovement (numbers) {
  // Declare count of increasing sequence
  var maxCount = 1;
  
  // Loop through numbers,
    // Check if the current number is greater than previous number,
    // If so, assign previous number equal to current number,
    // Increment counter
  if (numbers.length === 0) {
    
    return maxCount--;
    
  } else if (numbers.length === 1) {
    
    return maxCount;
    
  } else {
    
    var previousNumber = numbers[0];
    var currentCount = 1;
    
    for (var i = 1; i < numbers.length; i++) {
      if (numbers[i] > previousNumber) {
        previousNumber = numbers[i];
        currentCount++;
        maxCount = currentCount > maxCount ? currentCount : maxCount;
      } else {
        previousNumber = numbers[i];
        currentCount = 1;
      }
    }
    
    return maxCount;
  }
  
}
ready
Mike B
function longestImprovement (numbers) {
  var count = 1;
  var result = [];
  numbers.forEach(function(val,index){
      if(numbers[index]<numbers[index+1]){
         count++;
       } else {
         result.push(count);
         count = 1;
       }
  });

  var total = result.sort(function(a,b){return a>b}).pop()
  
  return total;
};
ready
Nissa
function longestImprovement (numbers) {
  var longestEver = 1;
  var currentLongest = 1;
  var prev = numbers[0];
  
  for (var i = 1; i < numbers.length; i++){
    var val = numbers[i]
    if (val > prev) {
      currentLongest++;
      if (currentLongest > longestEver){
      longestEver = currentLongest;
      }
    } else {
      currentLongest = 1; 
    }
    prev = val;
  }

  return longestEver; 
}
ready
Richard
function longestImprovement (numbers) {
  var longest = 1;
  var current = 1;
  for (var i = 1; i < numbers.length; i++) {
    if (numbers[i] > numbers[i-1]) {
      current++;
      if (current > longest) {
        longest = current;
      }
    } else {
      current = 1;
    }
  }
  return longest;
}
ready
Sepehr
function longestImprovement(numbers) {
  var length = 1, longest = 1;
  
  numbers.reduce(function(last, current) {
    length = last < current ? length+1 : 1;
    longest = length < longest ? longest : length;
    return current;
  });

  return longest;
}
ready
Zac
function longestImprovement (numbers) {
  if (!Array.isArray(numbers)) return null;
  if (numbers.length === 0) return 0;
  
  var length = 1;
  var longest = 1;
  
  numbers.reduce(function(last, num) {
    length = num > last ? length+1 : 1;
    if (length > longest) longest = length;

    return num;
  });
  
  return longest;
}
ready

Revisions

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

  • Revision 1: published by Greg Foley on