scramble

JavaScript performance comparison

Revision 15 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = ['aabecaa', 'abbbaac', 'aabejiabkfabed', '123224'];
    var answers = ['yes aa', 'no null', 'yes abe', 'no null'];
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
js1
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== PatternChaser(arr[i])) {
    console.log('Sam P failed test ' + i);
    throw 'failed test ' + i;
  }
}

function PatternChaser(str) {
  var i = 0;
  var strLength = str.length;
  var ptrMaxLength = Math.floor(strLength / 2);

  while (ptrMaxLength > 1) {
    testLength = 1 + strLength - (ptrMaxLength * 2);

    for (; i < testLength; i += 1) {
      if (str.indexOf(str.substr(i, ptrMaxLength), i + ptrMaxLength) > -1) {
        return "yes " + str.substr(i, ptrMaxLength);
      }
    }
    ptrMaxLength -= 1;
    i = 0;
  }
  return "no null";
}
pending…
js2
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== PatternChaser(arr[i])) {
    console.log('Sam P failed test ' + i);
    throw 'failed test ' + i;
  }
}

function PatternChaser(str) {
  var i = 0;
  var strLength = str.length;
  var ptrMaxLength = Math.floor(strLength / 2);
  var maxPosLength = (strLength + 1) - (ptrMaxLength + ptrMaxLength);

  while (ptrMaxLength > 1) {
    var testStr, passIndex = null;
    for (i = 0; i < maxPosLength; i += 1) {
      testStr = str.substr(i, ptrMaxLength);
      passIndex = i + ptrMaxLength - 1;
      if (str.lastIndexOf(testStr) > passIndex) {
        return "yes " + testStr;
      }
    }
    ptrMaxLength -= 1;
    maxPosLength += 2;
  }
  return "no null";
}
pending…
js3
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== PatternChaser(arr[i])) {
    console.log('Sam P failed test ' + i);
    throw 'failed test ' + i;
  }
}

function PatternChaser(str) {
  var strLength = str.length;
  var ptrMaxLength = Math.floor(strLength / 2);
  var maxPosLength = (strLength + 1) - (ptrMaxLength + ptrMaxLength);

  while (ptrMaxLength > 1) {
    var i, testStr, passIndex = null;
    for (i = 0; i < maxPosLength; i += 1) {
      testStr = str.substr(i, ptrMaxLength);
      passIndex = i + ptrMaxLength - 1;
      if (str.lastIndexOf(testStr) > passIndex) {
        return "yes " + testStr;
      }
    }
    ptrMaxLength -= 1;
    maxPosLength += 2;
  }
  return "no null";
}
pending…
js4
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== PatternChaser(arr[i])) {
    console.log('Sam P failed test ' + i);
    throw 'failed test ' + i;
  }
}

function PatternChaser(str) {
  var i = 0;
  var strLength = str.length;
  var ptrMaxLength = Math.floor(strLength / 2);
  var maxPosLength = (strLength + 1) - (ptrMaxLength + ptrMaxLength);

  while (ptrMaxLength > 1) {
    for (i = 0; i < maxPosLength; i += 1) {
      testStr = str.substr(i, ptrMaxLength);
      passIndex = i + ptrMaxLength - 1;
      if (str.lastIndexOf(testStr) > passIndex) {
        return "yes " + testStr;
      }
    }
    ptrMaxLength -= 1;
    maxPosLength += 2;
  }
  return "no null";
}
pending…
js5
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== PatternChaser(arr[i])) {
    console.log('Sam P failed test ' + i);
    throw 'failed test ' + i;
  }
}

function PatternChaser(str) {
  var strLength = str.length;
  var ptrMaxLength = Math.floor(strLength / 2);
  var maxPosLength = (strLength + 1) - (ptrMaxLength + ptrMaxLength);
  var yes = "yes ";
  var no = "no null";

  while (ptrMaxLength > 1) {
    var i, testStr, passIndex = null;
    for (i = 0; i < maxPosLength; i += 1) {
      testStr = str.substr(i, ptrMaxLength);
      passIndex = i + ptrMaxLength - 1;
      if (str.lastIndexOf(testStr) > passIndex) {
        return yes + testStr;
      }
    }
    ptrMaxLength -= 1;
    maxPosLength += 2;
  }
  return no;
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment