Cast To String

JavaScript performance comparison

Revision 10 of this test case created

Info

Fastest way to cast to a string.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function t1(x) {
      return String(x);
    }
   
    function t2(x) {
      return new String(x);
    }
   
    function t3(x) {
      return x + '';
    }
   
    function t4(x) {
      return x.toString();
    }
   
    function t5(x) {
      return '' + x;
    }
    var o = {
      toString: function() {
        return "87";
      }
    };
};
</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 Function
t1(87)
pending…
String Constructor
t2(87)
pending…
Concatenate Empty String
t3(87)
pending…
toString Method
t4(87)
pending…
Concatenate Empty String at start
t5(87)
pending…
Concate Empty String & toString Method
t3(o)
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