scramble

JavaScript performance comparison

Revision 10 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = ['Hello6 9World 2, Nic8e D7ay!', 'hello44444 world', 'wo192991xksi si_ ! $  si 89 zd 828 x', 'x129x idi xi 29 xi 291 2x james #smite', '129xxs#z@'];
    var answers = [2, 2, 5, 2, 3];
};
</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] !== NumberSearch(arr[i])) {
    console.log('Sam P failed test ' + i);
    throw 'failed test ' + i;
  }
}

function NumberSearch(str) {
  var n = 0;
  var strl = 0;

  for (var i = 0; i < str.length; i++) {
    code = str.charCodeAt(i)
    tdig = str[i];

    if (code > 48 && code < 58) n += parseInt(tdig);
    else if (code > 64 && code < 91) strl += 1;
    else if (code > 96 && code < 123) strl += 1;
  }
  return (Math.round((n / strl)));
}
pending…
js2
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== NumberSearch(arr[i])) {
    console.log('Sam P failed test ' + i);
    throw 'failed test ' + i;
  }
}

function NumberSearch(str) {
  var n = 0;
  var strl = 0;

  for (var i = 0; i < str.length; i += 1) {
    code = str.charCodeAt(i)
    tdig = str[i];

    if (code > 48 && code < 58) n += parseInt(tdig);
    else if (code > 64 && code < 91) strl += 1;
    else if (code > 96 && code < 123) strl += 1;
  }
  return (Math.round((n / strl)));
}
pending…
js3
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== NumberSearch(arr[i])) {
    console.log('Sam P failed test ' + i);
    throw 'failed test ' + i;
  }
}

function NumberSearch(str) {
  var n = 0;
  var strl = 0;

  for (var i = 0; i < str.length; i += 1) {
    code = str.charCodeAt(i)

    if (code > 48 && code < 58) n += parseInt(str[i]);
    else if (code > 64 && code < 91) strl += 1;
    else if (code > 96 && code < 123) strl += 1;
  }
  return (Math.round((n / strl)));
}
pending…
js4
for (var i = 0; i < arr.length; i++) {
  if (answers[i] !== NumberSearch(arr[i])) {
    console.log('Sam P failed test ' + i);
    throw 'failed test ' + i;
  }
}

function NumberSearch(str) {
  var n = 0;
  var strl = 0;

  for (var i = 0; i < str.length; i += 1) {
    code = str.charCodeAt(i)

    if (code > 48 && code < 58) n += parseInt(str[i]);
    else if (code > 64 && code < 91) strl += 1;
    else if (code > 96 && code < 123) strl += 1;
  }
  x = n / strl;
  return (x < 0 ? x - 0.5 >> 0 : x + 0.5 >> 0);
}
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