DOM vs JS

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

<script>
var els = [],
        els2 = [],
        arr = [],
        obj = {},
        max = 100;

for ( var i = 0; i < max; ++i ) {
        var el = document.createElement( 'div' );
        el.dataset.foo = i;
        document.body.appendChild( el );
        els.push( el );

        var el = document.createElement( 'div' );
        el.dataset.foo = i;
        document.body.appendChild( el );
        els2.push( el );

        arr.push( '' + i );
        obj[ 'k' + i ] = '' + i;
}
</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
DOM
els[ Math.random() * max | 0 ].dataset.foo = els[ Math.random() * max | 0 ].dataset.foo;
pending…
JS
arr[ Math.random() * max | 0 ] = arr[ Math.random() * max | 0 ];
pending…
DOM 2
els2[ Math.random() * max | 0 ].setAttribute( 'ata-foo', els2[ Math.random() * max | 0 ].getAttribute( 'ata-foo' ) );
pending…
JS 2
obj[ 'k' + ( Math.random() * max | 0 ) ] = obj[ 'k' + ( Math.random() * max | 0 ) ];
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