Duff's Device

JavaScript performance comparison

Test case created by Andrew J. Baker

Info

http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html#duffsdevice

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var testVal = 0;
    var iterations = 5125;
};
</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
Conventional Loop
for (var n = iterations; n > 0; --n) {
    testVal++;
    // do something else
}
pending…
Duff's Device
var n = iterations / 8;
var caseTest = iterations % 8;    

do {
    switch (caseTest) {
    case 0:
        testVal++;
        // do something else
    case 7:
        testVal++;
        // do something else
    case 6:
        testVal++;
        // do something else
    case 5:
        testVal++;
        // do something else
    case 4:
        testVal++;
        // do something else
    case 3:
        testVal++;
        // do something else
    case 2:
        testVal++;
        // do something else
    case 1:        
        testVal++;
        // do something else
    }
    caseTest = 0;
} while (--n > 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