Character comparison vs. number comparison

JavaScript performance comparison

Revision 3 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];
   
    var CHAR_A = 97;
    var CHAR_B = 98;
    var CHAR_C = 99;
    var CHAR_D = 100;
};
</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 (CHAR_A === x || CHAR_B === x || CHAR_C === x || CHAR_D === 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