sseletskyy
9/21/2016 - 10:07 AM

quick performance test of the function in js

quick performance test of the function in js

var testF = function(func, args){
  var counter = 100000, i = counter, startTimer, endTimer;
  startTimer = performance.now();
  for(;i>0;i--) {
    func.apply(null, args);
  }
  endTimer = performance.now();
  return (endTimer - startTimer) / counter;
}

var encrypt = function encrypt(text, n) {
  if (!text || text.length === 0 || n <= 0) return text;
  var index = 1, textLen = text.length, firstPart = [], secondPart = [];
  while (index < textLen) {
    firstPart.push(text[index]);
    secondPart.push(text[index-1]);
    index += 2;
  }
  if (index === textLen) {
    secondPart.push(text[textLen-1]);
  }
  return encrypt( firstPart.join('') + secondPart.join(''), n - 1);
}

var testString = "ABCDEFGHIJKLMNO123456789ABCDEFGHIJKLMNO123456789";
var result = testF(encrypt, [testString, 10]);

$('body').html("Everage time: " + result + " milliseconds.");