JS Challenge 3

JavaScript performance comparison

Revision 2 of this test case created

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
test
// async test
function NumberSearch(str) {
  var n = str.split('');
  var number = 0;
  var letter = 0;

  for (var i = 0; i < n.length; i++) {
    if (!isNaN(n[i])) {
      if (n[i] != ' ') {
        number = number + parseInt(n[i]);
      }
    } else {
      letter = letter + 1;
    }
  }

  var result = Math.round(number / letter);
  alert('Number:' + number);
  alert('Letter:' + letter);
  return result;
}

document.write(NumberSearch('Hello6 9World 2, Nic8e D7ay!'));
pending…
test
function NumberSearch(str) {
  var n = str.split('');
  var number = 0;
  var letter = 0;

  for (var i = 0; i < n.length; i++) {
    if (!isNaN(n[i])) {
      if (n[i] != ' ') {
        number = number + parseInt(n[i]);
      }
    } else {
      letter = letter + 1;
    }
  }

  var result = Math.round(number / letter);
  alert('Number:' + number);
  alert('Letter:' + letter);
  return result;
}

document.write(NumberSearch('Hello6 9World 2, Nic8e D7ay!'));
pending…
Max. H
function NumberSearch(str) {

  var len = str.length;
  var i = 0;
  var count = 0;
  var curCharCode;
  var total = 0;

  for (i; i < len; i++) {
    curCharCode = str.charCodeAt(i);

    if ((curCharCode > 64 && curCharCode < 91) || (curCharCode > 96 && curCharCode < 123)) {
      count++;
    }

    if (curCharCode > 47 && curCharCode < 58) total += parseInt(str[i], 10);
  }

  var result = Math.round(total / count);
  return result;
}
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