foreach-vs-for-loop

JavaScript performance comparison

Test case created by 祁涛

Info

sfs

Preparation code

<div id="scene-top" class="scene"></div>
<div id="scene-concept">
        <div id="concept01" class="scene"></div>
        <div id="concept02" class="scene"></div>
        <div id="concept03" class="scene"></div>
        <div id="concept04" class="scene"></div>
</div>
<div id="scene-mote" class="scene"></div>
<div id="scene-contact" class="scene"></div>
<div id="scene-company" class="scene"></div><script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var $scenes = $( '.scene' );
    var scenes = $( '.scene' ).toArray();
    var scenesLength = scenes.length;
};
</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
forEach
$scenes.each( function() {
        var $this = $( this );
        var offsetTop = $this.offset().top;
} );
pending…
javascript style
for( var i = 0; i < scenesLength; i++ ){
        var scene = scenes[ i ];
        var offsetTop = scene.offsetTop;
}
pending…
jQuery style 2
$scenes.each( function() {
        var $this = $( this );
        var offsetTop = $this.css( 'top' );
} );
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