Duff's Device

JavaScript performance comparison

Test case created

Info

To see if Duff's Device has significant performance advantages with large number of iterations in modern JS engines.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var divs500k = [];
    var divs1m = [];
    var i;
   
    for (i = 0; i < 500,000; i++) {
        divs500k[i] = document.createElement('div');
        document.body.appendChild(divs500k[i]);
    }
   
    for (i = 0; i < 1,000,000; i++) {
        divs1m[i] = document.createElement('div');
        document.body.appendChild(divs1m[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
500,000 iterations no Duff's Device
for (var i = 0; i < divs500k.length; i++) {
    divs500k[i].style.background = 'black';
}
pending…
500,000 iterations with Duff's Device
var i = divs500k.length % 8;

while(i){
    divs500k[i--].style.background = 'black';
}

i = Math.floor(divs500k.length / 8);

while(i){
    divs500k[i--].style.background = 'black';
    divs500k[i--].style.background = 'black';
    divs500k[i--].style.background = 'black';
    divs500k[i--].style.background = 'black';
    divs500k[i--].style.background = 'black';
    divs500k[i--].style.background = 'black';
    divs500k[i--].style.background = 'black';
    divs500k[i--].style.background = 'black';
}
pending…
1,000,000 iterations no Duff's Device
for (var i = 0; i < divs1m.length; i++) {
    divs1m[i].style.background = 'black';
}
pending…
1,000,000 iterations with Duff's Device
var i = divs1m.length % 8;

while(i){
    divs1m[i--].style.background = 'black';
}

i = Math.floor(divs1m.length / 8);

while(i){
    divs1m[i--].style.background = 'black';
    divs1m[i--].style.background = 'black';
    divs1m[i--].style.background = 'black';
    divs1m[i--].style.background = 'black';
    divs1m[i--].style.background = 'black';
    divs1m[i--].style.background = 'black';
    divs1m[i--].style.background = 'black';
    divs1m[i--].style.background = 'black';
}
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