scramble

JavaScript performance comparison

Revision 3 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = [
      ['lehlowww', 'hello'],
      ['helio', 'hell']
    ];
};
</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++) {
  overload(arr[i][0], arr[i][1]);
}

function overload(str1, str2) {
  if (str2.length > str1.length) return false;
  var i = 0;
  var arr1 = [];
  for (var i = 0, length = str1.length; i < length; i++) {
    arr1.push(str1.charCodeAt(i));
  }

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

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

  return true;
}
pending…
js2
for (var i = 0; i < arr.length; i++) {
  overload(arr[i][0], arr[i][1]);
}

function overload(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));
  }

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

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

  return true;
}
pending…
js3
for (var i = 0; i < arr.length; i++) {
  overload(arr[i][0], arr[i][1]);
}

function overload(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));
  }

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

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

  return true;
}
pending…
js4
for (var i = 0; i < arr.length; i++) {
  StringScramble(arr[i][0], arr[i][1]);
}

function StringScramble(str1, str2) {
  var arr = new Array(26);
  var index;
  var arr_length = arr.length;
  var str1_length = str1.length;
  var str2_length = str2.length;
  // Initialize the array to 0
  for (var i = 0; i < arr_length; i++) {
    arr[i] = 0;
  }

  str2 = str2.toLowerCase();
  // Put char counts into arr for str2
  for (var j = 0; j < str2_length; j++) {
    index = str2[j].charCodeAt(0) - 97;
    arr[index] = arr[index] + 1;
  }

  // Compare str1 to str2
  for (var k = 0; k < str1_length; k++) {
    index = str1[k].charCodeAt(0) - 97;
    if (arr[index] > 0) arr[index] = arr[index] - 1;
  }

  // If any of the elements in the arry is non 0,
  // there's a mismatch and str1 cannot be a permutation of str2
  for (var n = 0; n < arr_length; n++) {
    if (arr[n] != 0) 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