Primitive value string vs Wrapper Object String - length

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

str1.length = <div id="id1"></div><br>
str2.length = <div id="id2"></div>
Benchmark.prototype.setup = function() {
    var str11 = Math.random().toString();
    var str12 = Math.random().toString();
    var str21 = new String(str11);
    var str22 = new String(str12);
    var i = "(idle)",
        k = "(idle)";

Benchmark.prototype.teardown = function() {
    var x = document.getElementById("id1"); x.innerHTML = i;
    var y = document.getElementById("id2"); y.innerHTML = k;

Preparation code output

str1.length =

str2.length =

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Primitive value string str1.length
i = str11.length;
var tmp = str11; str11 = str12; str12 = tmp;
Wrapper object string str2.length
k = str21.length;
var tmp = str21; str21 = str22; str22 = tmp;

Compare results of other browsers


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:


Comment form temporarily disabled.

Add a comment