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

JavaScript performance comparison

Revision 10 of this test case created by Unkn0wn

Info

This is a revert of an older test to compare a prototype to a user defined type and with the String type.

The test shows that adding a prototype to a system type such as String is much slower than adding a prototype to a user type.

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);
    };
   
    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;
};
</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
(String)Prototype - mul
output = input.UInt16_a(0);
pending…
(String)Prototype - shift
output = input.UInt16_b(0);
pending…
Prototype - mul
output = u16_c.func(input, 0);
pending…
Prototype - shift
output = u16_d.func(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