String to UInt16 (v2)

Revision 2 of this benchmark created on


Setup

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;

Test runner

Ready to run.

Testing in
TestOps/sec
Prototype - mul
output = input.UInt16_a(0);
ready
Prototype - shift
output = input.UInt16_b(0);
ready
Function - mul
output = UInt16_c(input, 0);
ready
Function - shift
output = UInt16_d(input, 0);
ready
Inline - mul
output = input.charCodeAt(n) + 256 * input.charCodeAt(n + 1);
ready
Inline - shift
output = input.charCodeAt(n) + (input.charCodeAt(n + 1) << 8);
ready
Object - mul
inputObject.UInt16_a(0);
ready
Object - shift
inputObject.UInt16_b(0);
ready
Lookuptable
UInt16_e( input, 0 );
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.