Duff's device

Benchmark created by Miller Medeiros on


Preparation HTML

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

Test runner

Ready to run.

Testing in
TestOps/sec
Regular Loop
var testVal = 0;
var n = iterations;
while (n--) {
 testVal++;
}
ready
Fast Duff's Device #3
/*
 * 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++;
}
ready
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);
ready
Fast Duff's Device #1
/**
 * 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++;
}
ready
Fast Duff's Device #2
/*
 * 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++;
}
ready
Fast Duff's Device #4
/*
 * 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++;
}
ready

Revisions

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