Duff's device

JavaScript performance comparison

Revision 33 of this test case created by Jörg Schaper

Preparation code

<script>
  var iterations = 12345;
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Regular Loop 1
var testVal = 0;
var n = iterations;
while (n--) {
 testVal++;
}
pending…
Regular Loop 2
// 2014-01-20 Joerg_Schaper@gmx.de
var v = 0, i = iterations;
while(i>0){
 i--;
 v++;
};
pending…
Duff's Device
/**
 * Duff's Device 
 * http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html#duffsdevice
 */
var testVal = 0;
var n = iterations / 8;
var caseTest = iterations % 8;

do {
 switch (caseTest) {
 case 0:
  testVal++;
 case 7:
  testVal++;
 case 6:
  testVal++;
 case 5:
  testVal++;
 case 4:
  testVal++;
 case 3:
  testVal++;
 case 2:
  testVal++;
 case 1:
  testVal++;
 }
 caseTest = 0;
}
while (--n > 0);
pending…
Fast Duff's Device #01
/**
 * Fast Duff's Device
 * @author Jeff Greenberg
 * http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html#fastDuffsdevice
 */
var testVal = 0;
var n = iterations % 8;
while (n--) {
 testVal++;
}

n = parseInt(iterations / 8);
while (n--) {
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
}
pending…
Fast Duff's Device #02
/*
 * Fast Duff's Device
 * @author Miller Medeiros <http://millermedeiros.com> 
 * @version 0.1 (2010/08/25)
 */
var testVal = 0;
var n = iterations % 8;
while (n--) {
 testVal++;
}

n = Math.floor(iterations / 8);
while (n--) {
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
}
pending…
Fast Duff's Device #03
/*
 * Fast Duff's Device
 * @author Miller Medeiros <http://millermedeiros.com> 
 * @version 0.2 (2010/08/25)
 */
var testVal = 0;
var n = iterations % 8;
while (n--) {
 testVal++;
}

n = (iterations / 8) ^ 0; //`value ^ 0` is the same as `Math.floor` for positive numbers and `Math.ceil` for negative numbers
while (n--) {
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
}
pending…
Fast Duff's Device #04
/*
 * Fast Duff's Device
 * @author Miller Medeiros <http://millermedeiros.com> 
 * @version 0.3 (2010/08/25)
 */
var testVal = 0;
var n = iterations % 8;
while (n--) {
 testVal++;
}

n = (iterations * 0.125) ^ 0; //multiplication is faster than division in some cases
while (n--) {
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
 testVal++;
}
pending…
JS.duffsDevice.008
// 2014-01-20 Joerg_Schaper@gmx.de
var v = 0, i = iterations;
while(i>7){i-=8;v++;v++;v++;v++;v++;v++;v++;v++;};
switch(i){case 7:v++;case 6:v++;case 5:v++;case 4:v++;case 3:v++;case 2:v++;case 1:v++;};
pending…
JS.duffsDevice.016
// 2014-01-20 Joerg_Schaper@gmx.de
var v = 0, i = iterations;
while(i>15){
 i-=16;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
};
while(i>7){i-=8;v++;v++;v++;v++;v++;v++;v++;v++;};
switch(i){case 7:v++;case 6:v++;case 5:v++;case 4:v++;case 3:v++;case 2:v++;case 1:v++;};
pending…
JS.duffsDevice.032
// 2014-01-20 Joerg_Schaper@gmx.de
var v = 0, i = iterations;
while(i>31){
 i-=32;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
};
while(i>7){i-=8;v++;v++;v++;v++;v++;v++;v++;v++;};
switch(i){case 7:v++;case 6:v++;case 5:v++;case 4:v++;case 3:v++;case 2:v++;case 1:v++;};
pending…
JS.duffsDevice.064
// 2014-01-20 Joerg_Schaper@gmx.de
var v = 0, i = iterations;
while(i>63){
 i-=64;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
};
while(i>7){i-=8;v++;v++;v++;v++;v++;v++;v++;v++;};
switch(i){case 7:v++;case 6:v++;case 5:v++;case 4:v++;case 3:v++;case 2:v++;case 1:v++;};
pending…
JS.duffsDevice.128
// 2014-01-24 Joerg_Schaper@gmx.de
var v = 0, i = iterations;
while(i>127){
 i-=128;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++; 
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
 v++;v++;v++;v++;v++;v++;v++;v++;
};
while(i>7){i-=8;v++;v++;v++;v++;v++;v++;v++;v++;};
switch(i){case 7:v++;case 6:v++;case 5:v++;case 4:v++;case 3:v++;case 2:v++;case 1:v++;};
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.

0 Comments