for-loop-test

JavaScript performance comparison

Test case created by Volkan Ozcelik

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var Model = function(str) {
        this.str = str;
    };
   
    Model.prototype.toJSON = function() {
        return {val:"foo"+this.str};
    };
   
    var models = [];
   
    var $div = $('<div/>');
   
    for(var i=0; i<1000;i++) {
        models.push( new Model("baz"+i) );
    }
};

Benchmark.prototype.teardown = function() {
    models = [];
   
    $div.html('');
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
render model outside loop
function renderModel( model ) {
    var data = model.toJSON();

    data.baz = "xl";

    $div.append(
        $( "<li>" + data.val + "</li>" )
    );
}

for ( i = 0, len = models.length; i < len; i++ ) {
    renderModel( models[ i ] );
}
pending…
render model inside loop

for ( i = 0, len = models.length; i < len; i++ ) {
    var model = models[ i ];
    var data = model.toJSON();

    data.baz = "xl";

    $div.append(
        $( "<li>" + data.val + "</li>" )
    );
}
pending…

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

Compare results of other browsers

0 comments

Add a comment