String to UInt16

JavaScript performance comparison

Revision 2 of this test case created

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 UInt16_e = function(s, n) {
        return luTable[s[n]] + ( luTable[s[n]] << 8);
        };
   
    var luTable = {}, l = 256;
   
    while( l-- ) {
    luTable[String.fromCharCode(l)] = l;
    }
    var input = "Ax";
    var inputObject = new String( "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…
Object - mul
inputObject.UInt16_a(0);
pending…
Object - shift
inputObject.UInt16_b(0);
pending…
Lookuptable
UInt16_e( input, 0 );
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