MPC Zombie Apocalypse

JavaScript performance comparison

Test case created by Joshua Piccari

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function sduket(n, z) {
        if (n == 1) {
            return 1; //"1 Man, 1 Million dead, The odds are just about even."
   
        } else {
   
            //initialize 2D array to immitate circular doubly linked list. Values represent current ID, previous ID, next ID
            var shifts = [
                [0, n - 1, 1]
            ]; //special! first shift's 'previous' is the last shift
            for (var x = 1; x < (n - 1); x++) {
                shifts.push([x, x - 1, x + 1]);
            }
            shifts.push([n - 1, n - 2, 0]); //special! Last shift's 'next' is the first shift. Loop activate!
   
            var current = 0; //current person on shift
            var count = 0; //count down to next zombification
           
            //While next on shift is not the same as current (i.e. there is only one person left).
            while (shifts[current][2] != shifts[current][0]) {
                if (count == z - 1) { //if countdown is up
                    //adjust pointers to skip this ID in future passes
                    var prevshift = shifts[current][1];
                    var nextshift = shifts[current][2];
                    shifts[prevshift][2] = shifts[current][2];
                    shifts[nextshift][1] = shifts[current][1];
   
                    //reset count down
                    count = 0;
                } else {
                    count++;
                }
                current = shifts[current][2]; //the next shift begins
            }//end while loop
            return (shifts[current][0] + 1);
        }
    }
   
    function jarmstrong(n, z) {
        var people = Array.apply(0, Array(n)).map(function() { return arguments[1] + 1; }),
            nextDeath = z - 1;
       
        while (people.length !== 1) {
            people.splice(nextDeath, 1);
            nextDeath = (nextDeath + z - 1) % people.length;
        }
       
        return people[0];
    }
   
    function jramey(n, z) {
       // Go nuts, this is your function
        // Zombie puzzle solution by Jake Ramey.
        // Testing for jackasses putting in a single survivor.
        if (n == 1) return 1;
        // Creating an array to track survivors.
        var survivors = [];
        for (i = 0; i < n; i++){
            survivors[i] = i + 1;
        }
        // Remove infected survivors when needed.
        function zombify() {
            survivors.splice(0,1);
        }
        function notZombie(){
            survivors.splice(survivors.length,0,survivors[0]);
            survivors.splice(0,1);
        }
        // Looping to grind down an array.
        while (survivors.length > 1){
            for (i = 1; i < z; i++){
                notZombie();
            }
            // Zombifying the lucky human on doom shift, then begining the next cycle.
            zombify();
        }
        // Returning the shift location of the sole survivor.
        return survivors[0];
    }
   
    function anelson(n, z) {
        var humans = [];
        for (var i = 0; i < n; i++) {
                humans.push(i + 1)
        }
        var shiftCounter = 0;
        while (humans.length > 1) {
                var theHuman = humans.shift();
                shiftCounter++;
                if (shiftCounter == z) {
                        shiftCounter = 0
                } else {
                        humans.push(theHuman)
                }
        }
        return humans[0]
    }
   
    function jnelson(n, z) {
        var people = [],
            shiftNumber = 1;
       
        people = Array.apply(0, new Array(n)).map(function(val, idx) {
            return idx + 1;
        });
        // I wanted to do:
        // (new Array(n)).map( function(val, idx) { return idx+1; } );
        // but that doesn't actually iterate over the array, so we had
        // to initialize every entry to 0
        // this is slow, but awesome :)
       
        while (people.length > 1) {
            if (shiftNumber % z === 0) {
                // zombie!
                people.shift();
            } else {
                // safe
                people.push(people.shift());
            }
            shiftNumber++;
        }
   
        return people[0];
    }
   
    function jwhitbred(n, z) {
        // Jeremy's One Line Solution:
        return (n > 1) ? (jwhitbred(n - 1, z) + z - 1) % n + 1: 1;
    }
   
    function jedwards(n, z) {
        var people = [];
        var nextToDie=(z-1) % n;
       
        if (n < 1 || z < 1){ // handle edge cases with no people or zombies
            return;
        }
       
        //create array of survivors
        for (var j=1; j<=n; j++){
            people.push(j);
        }
   
        //loop through and delete every z-th array element until one remains
        while (people.length > 1){
            people.splice(nextToDie, 1);
            nextToDie += z-1;
            nextToDie %= people.length; //when count to next > array length, find remainder
        }
        return people[0]; //volunteer for this shift
    }
   
    function hkim(n, z) {
       var people = [],
            firstOne,
            i,
            shift;
           
        //create people array
        for(var num=1; num<=n; num++){
            people.push(num);    
        }
       
        //shift people for n-1 times
        for(shift=1; shift<n; shift++){
           
            //Once the person is done with the shift, move to the last in the queue.
            for(i=0;  i<z; i++){
                //get the first person
                firstOne = people[0];
               
                //remove the first person
                people.shift();
               
                //move the first one to the last
                people.push(firstOne);
            }
            //Remove the last person infected from the queue.  
            people.pop();        
        }
        //Get the last person live.
        return people[0];
    }
};
</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
S. Duket
sduket(189, 51)
pending…
J. Armstrong
jarmstrong(189, 51)
pending…
J. Ramey
jramey(189, 51)
pending…
A. Nelson
anelson(189, 51)
pending…
J. Nelson
jnelson(189, 51)
pending…
J. Whitbred
jwhitbred(189, 51)
pending…
J. Edwards
jedwards(189, 51)
pending…
H. Kim
hkim(189, 51)
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