String to UInt16

Benchmark created by Alnitak 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 input = "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

Revisions

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