JS Challenge 3

JavaScript performance comparison

Revision 4 of this test case created by Scott

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var str = "Wh3t 1s th3 3 tes";
   
   
};

Benchmark.prototype.teardown = function() {
    if (Answer !== 1) {
        console.log('you failed');
    };
};
</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
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;
}

Answer=NumberSearch(str);
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;
}

Answer=NumberSearch(str);
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;
}

Answer=NumberSearch(str);
pending…
Scott
function NumberSearch(str) {
  var sum = 0, num = 0, len = str.length, i = 0, code;
  for(;i<len;i += 1) {
    code = str.charCodeAt(i);
    if ((code > 64 && code < 91) || (code > 96 && code < 123)) {
      num += 1;
    } else if (code > 47 && code < 58) {
      sum += parseInt(str[i]);
    }
  }
  return Math.round(sum / num);
}

Answer=NumberSearch(str);
pending…
Alex L
function NumberSearch2(str) {
    if (typeof str == 'string') {
        var chars = str.replace(/[^\w]/g, ""),
            nums = chars.replace(/[^\d]/g, ""),
            total = 0;
 
        for(var i=0, e=nums.length; i<e; i++) {
            total += parseInt(nums[i]);
        }
       
        return Math.round( total/(chars.length-e) );
    }
   
    return false;
}

Answer=NumberSearch2(str);
pending…
Sam
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) || (code > 96 && code < 123))
strl += 1;
}
return (Math.round((n/strl)));
}

Answer=NumberSearch(str);
pending…
David K
function NumberSearch(str) {

var strArr = str.split('');
var numArr = [];
var numTotal = letTotal = 0;

for (var i = 0; i < strArr.length; i++) {
if (strArr[i].search(/^[A-Za-z]/) > -1) {
letTotal++;
}
if (!isNaN(strArr[i]) && (strArr[i]!=" ")) {
numTotal += (parseInt(strArr[i]));
}
}
return Math.round(numTotal/letTotal);
}

Answer=NumberSearch(str);
pending…
Chin
function NumberSearch(str){
var letter = str.replace(/[0-9\.,-\/#!$%\^&\*;:{}=\-_`~() ]/g,"");
var number = str.replace(/[a-zA-Z\.,-\/#!$%\^&\*;:{}=\-_`~() ]/g,"");
var n = 0;

for (var i=0; i< number.length; i++)
{
n = n + parseInt(number[i]);
}

var result = Math.round(n / letter.length);
return result;
}

Answer=NumberSearch(str);
pending…
Jenni
function NumberSearch(str){
  var l = str.length,
      n=0,
      a=0;
  for(var i=0; i< l; i++){
    var cc = str.charCodeAt(i);
    if(cc>47 && cc<58){
      n += parseInt(str[i]);
    }else if((cc>64 && cc<91) || (cc>96 && cc<123)){
      a++;
    }else{
    }
  }
    return Math.round(n/a);
}

Answer=NumberSearch(str);
pending…
Patrick
var alpha_arr = ["e", "t", "a", "o", "i", "n", "s", "h", "r", "d", "l", "c", "u", "m", "w", "f", "g", "y", "p", "b", "v", "k", "j", "x", "q", "z"];

function isInt(character){
  return parseInt(character);
}

function NumberSearch(str1){
  str1 = str1.toLowerCase()
  var numbers = 0;
  var letters = 0;
  var arr = str1.split("");

  for(var i=0; i < arr.length; i++){
    var val = isInt(arr[i]);
    if(val){ numbers += val; }
    else if(alpha_arr.indexOf(arr[i]) >= 0){
      letters += 1;
    }
  }
 
  if(numbers.length == 0){ return 0; }
  return Math.round(numbers/letters);
 
}

Answer=NumberSearch(str);
pending…
Best
function NumberSearch(str) {
  var sum = 0, num = 0, len = str.length, i = 0, code;
  for(;i<len;i += 1) {
    code = str.charCodeAt(i);
    if ((code > 64 && code < 91) || (code > 96 && code < 123)) {
      num += 1;
    } else if (code > 47 && code < 58) {
      sum += parseInt(str[i], 10);
    }
  }
  return Math.round(sum / num);
}

Answer=NumberSearch(str);
pending…
Not Better
function NumberSearch(str) {
  var sum = 0, num = 0, len = str.length, i = 0, code;
  for(;i<len;i += 1) {
    code = str.charCodeAt(i);
    if ((code > 64 && code < 91) || (code > 96 && code < 123)) {
      num += 1;
    } else if (code > 47 && code < 58) {
      sum -= -str[i];
    }
  }
  return Math.round(sum / num);
}

Answer=NumberSearch(str);
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