getElementByClass: for vs for..in

Benchmark created by 51L3N7 on


Preparation HTML

<script>
HTMLDocument.prototype.getElementByClass = function( className ) {
        for ( i = 0, j = document.getElementsByTagName( '*' ); i++ < j.length; ) if ( j[i].className == className ) return j[i];
}

HTMLDocument.prototype.getElementByClass2 = function( className ) {
        for ( i in j = document.getElementsByTagName( '*' ) ) if ( j[i].className == className ) return j[i];
}
</script>

<p class="teste">Texto de teste</p>

Test runner

Ready to run.

Testing in
TestOps/sec
for
document.getElementByClass( 'teste' ).innerHTML = 'Novo texto de teste.';
ready
for..in
document.getElementByClass2( 'teste' ).innerHTML = 'Novo texto de teste.';
ready

Revisions

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

  • Revision 1: published by 51L3N7 on