scramble

JavaScript performance comparison

Revision 5 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = [
      ['lehlowww', 'hello'],
      ['dlrwxdonexfe', 'oneworld'],
      ['oenhodwrol', 'oneworld'],
      ['rkqodlw', 'world'],
      ['olaelh', 'hello'],
      ['helio', 'hell'],
      ['abcewnorde', 'wonder'],
      ['wioskrngatwo', 'workingtwo'],
      ['locaacedo', 'locatedhere'],
      ['edfbenowaginning', 'beginningnow'],
      ['CAPS TEST', 'caps test'],
      ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent et mauris magna, ac dapibus ipsum. Sed viverra feugiat vehicula. Pellentesque vel lacinia ipsum. Vestibulum vel diam vel velit mollis adipiscing. Suspendisse potenti. Duis elementum erat vitae neque blandit porttitor. Pellentesque pellentesque vulputate posuere. Pellentesque eget justo elit. Praesent elementum odio sit amet lectus dignissim tincidunt. In quam massa, varius nec convallis quis, luctus sed erat. Cras ut massa metus. Integer lacinia iaculis pretium.', 'luctus sed erat']
    ];
   
    var answers = [true, false, true, true, true, false, true, true, false, true, false, true];
};
</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] !== StringScramble(arr[i][0], arr[i][1])) {
    console.log('Sam P failed test ' + i);
    throw 'failed test ' + i;
  }
}

function StringScramble(str1, str2) {
  if (str2.length > str1.length) return false;

  str1 = str1.split("");
  str2 = str2.split("");

  for (var i = 0; i < str2.length; i++) {
    if ((x = str1.indexOf(str2[i])) > -1) str1[x] = -1;
    else return false;
  }

  return true;
}
pending…
js2
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== StringScramble(arr[i][0], arr[i][1])) {
    console.log('Sam P failed test ' + i);
    throw 'failed test ' + i;
  }
}

function StringScramble(str1, str2) {
  if (str2.length > str1.length) return false;

  str1 = str1.split("");
  str2 = str2.split("");

  for (var i = 0; i < str2.length; i += 1) {
    if ((x = str1.indexOf(str2[i])) > -1) str1[x] = -1;
    else return false;
  }

  return true;
}
pending…
js3
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== StringScramble(arr[i][0], arr[i][1])) {
    console.log('Sam P failed test ' + i);
    throw 'failed test ' + i;
  }
}

function StringScramble(str1, str2) {
  if (str2.length > str1.length) return false;

  str1 = str1.split("");

  for (var i = 0; i < str2.length; i++) {
    if ((x = str1.indexOf(str2[i])) > -1) str1[x] = -1;
    else return false;
  }

return true;
}
pending…
js4
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== StringScramble(arr[i][0], arr[i][1])) {
    console.log('Sam P failed test ' + i);
    throw 'failed test ' + i;
  }
}

function StringScramble(str1, str2) {
                if (str2.length > str1.length) return false;

                var arr1 = [];
                for(var i = 0, length = str1.length; i < length; i++) {
                        arr1.push(str1.charCodeAt(i));
                }

                for (i = 0; i < str2.length; i++) {
                        if ((x = arr1.indexOf(str2.charCodeAt(i))) > -1) arr1[x] = -1;
                        else return false;
                }

                return true;
}
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