Switch char vs number

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var s = "1234567890abcdefghijklmon";
};
</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
Strings
var s = "1234567890abcdefghijklmon";

var h = null
for (var i = 0; i < s.length; i++) {

  switch (s[i]) {
    case 'A':
      h = s[i];
      break;
    case 'a':
      h = s[i];
      break;
    case 'b':
      h = s[i];
      break;
    case 'c':
      h = s[i];
      break;

  }

}
pending…
numbers
var s = "1234567890abcdefghijklmon";

var h = null
for (var i = 0; i < s.length; i++) {

  switch (s.charCodeAt(i)) {
    case 65:
      h = 65;
      break;
    case 97:
      h = 97;
      break;
    case 98:
      h = 98;
      break;
    case 99:
      h = 99;
      break;

  }

}
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