String literal vs Object cast (v10)

Revision 10 of this benchmark created on


Setup

var literals, values = [], objects = [];
    
    for(var i = 0; i < 1000; i++) {
        var s = Math.random().toString().substring(0, 10);
        values.push(s.valueOf());
        objects.push(s);
    }
    
    literals = (new Function('return [\'' + objects.join('\',\'') + '\']'))();
    
    String.prototype.work = function() {
        return this.charAt(2);
    }

Teardown


    delete String.prototype.work;
  

Test runner

Ready to run.

Testing in
TestOps/sec
String Literal
for(var i = 0; i < 1000; i++) {
    literals[i].work();
}
ready
String Value
for(var i = 0; i < 1000; i++) {
    values[i].work();
}
ready
String Object
for(var i = 0; i < 1000; i++) {
    objects[i].work();
}
ready

Revisions

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