String to UInt16

JavaScript performance comparison

Test case created by Alnitak

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    String.prototype.UInt16_a = function(n) {
      return this.charCodeAt(n) + 256 * this.charCodeAt(n + 1);
    };
   
    String.prototype.UInt16_b = function(n) {
      return this.charCodeAt(n) + (this.charCodeAt(n + 1) << 8);
    };
   
    var UInt16_c = function(s, n) {
        return s.charCodeAt(n) + 256 * s.charCodeAt(n + 1);
        };
   
    var UInt16_d = function(s, n) {
        return s.charCodeAt(n) + (s.charCodeAt(n + 1) << 8);
        };
   
    var input = "Ax";
    var output = 0;
    var n = 0;
};
</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
Prototype - mul
output = input.UInt16_a(0);
pending…
Prototype - shift
output = input.UInt16_b(0);
pending…
Function - mul
output = UInt16_c(input, 0);
pending…
Function - shift
output = UInt16_d(input, 0);
pending…
Inline - mul
output = input.charCodeAt(n) + 256 * input.charCodeAt(n + 1);
pending…
Inline - shift
output = input.charCodeAt(n) + (input.charCodeAt(n + 1) << 8);
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