String to UInt16 (comparing adding a prototype to string and user type) (v10)

Revision 10 of this benchmark created by Unkn0wn 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);
  };
  
  function UInt16_c() {}
  UInt16_c.prototype.func = function(s, n) {
      return s.charCodeAt(n) + 256 * s.charCodeAt(n + 1);
      };
  
  function UInt16_d() {}
  UInt16_d.prototype.func = function(s, n) {
      return s.charCodeAt(n) + (s.charCodeAt(n + 1) << 8);
      };
  var u16_c = new UInt16_c();
  var u16_d = new UInt16_d();
  
  var input = "Ax";
  var output = 0;
  var n = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
(String)Prototype - mul
output = input.UInt16_a(0);
ready
(String)Prototype - shift
output = input.UInt16_b(0);
ready
Prototype - mul
output = u16_c.func(input, 0);
ready
Prototype - shift
output = u16_d.func(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.