scramble

JavaScript performance comparison

Revision 13 of this test case created and last updated

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = ['Hello6 9World 2, Nic8e D7ay!'];
    var answers = [2];
};
</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;
    var len = str.length;
                for(var i = 0; i < len; i+=1)
                {
                        code = str.charCodeAt(i)

                        if ( code > 48 && code < 58)
                                n += parseInt(str[i]);
                        else if ((code > 64 && code < 91) || (code > 96 && code < 123) )
                                strl += 1;
                }
                return (Math.round((n/strl)));
        }
pending…
js5
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], 10);
                        else if ((code > 64 && code < 91) || (code > 96 && code < 123) )
                                strl += 1;
                }
                return (Math.round((n/strl)));
        }
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