Character comparison vs. number comparison

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var characters = ["q","w","e","r","t","y","u","i","o","p","[","]","a","s","d","f","g","h","j","k","l",";","\'","z","x","c","v","b","n","m",",",".","/"];
   
    var numbers = [113,119,101,114,116,121,117,105,111,112,91,93,97,115,100,102,103,104,106,107,108,59,39,122,120,99,118,98,110,109,44,46,47];
};
</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
Character
var counter = 0, index, length, x;

for (index = 0, length = characters.length; index < length; index += 1) {
  x = characters[index];
  if ('a' === x || 'b' === x || 'c' === x || 'd' === x) {
    counter += 1;
  }
}

return counter;
pending…
Number
var counter = 0, index, length, x;

for (index = 0, length = numbers.length; index < length; index += 1) {
  x = numbers[index];
  if (97 === x || 98 === x || 99 === x || 100 === x) {
    counter += 1;
  }
}

return counter;
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