chair ring

JavaScript performance comparison

Revision 7 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  if (!Array.prototype.filter)
  {
    Array.prototype.filter = function(fun /*, thisp */)
    {

      "use strict";
   
      if (this == null)
        throw new TypeError();
   
      var t = Object(this);
      var len = t.length >>> 0;
      if (typeof fun != "function")
        throw new TypeError();
   
      var res = [];
      var thisp = arguments[1];
      for (var i = 0; i < len; i++)
      {
        if (i in t)
        {
          var val = t[i]; // in case fun mutates this
          if (fun.call(thisp, val, i, t))
            res.push(val);
        }
      }
   
      return res;
    };
  }

};
</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
array
	
	// setup array
	var chairArr = [];
	for (var i = 1; i <= 100; i++)
		chairArr.push(i);

	// whether to remove odd or even chairs
	var odd = 0;

	// while there are still chairs
	while (chairArr.length > 1) {

		// remember the initial length
		var last = chairArr.length;

		// filter odd or even chairs
		chairArr = chairArr.filter(function (val, ind) {
			return (ind + odd) % 2
		});

		// used to decide odd or even chairs next round
		odd += last;
	}

	// return the answer
	chairArr[0];

pending…
custom data structure
// setup chair ring
var start = {num:1};
var one = start;
for(var i = 2; i <= 100; i++) {
	var temp = {num:i};
	start.next = temp;
	start = temp;	
}
start.next = one;
one = null;

// go around removing chairs
while(start.next != start) {
	start.next = start.next.next;
	start = start.next;
}
start.num;
pending…
array sol'n 2
	
	// setup array
	var chairArr = [];
	for (var i = 1; i <= 100; i++)
		chairArr.push(i);

	for (i = 1; i < chairArr.length; i+=2)
    chairArr.push(chairArr[i]);

	

	// return the answer
	chairArr.pop();
pending…
regex
// setup array
var chairs = (new Array(101).join('1'));

var last = odd = chairs.length;
var mask = ['0$11', '1$10'];
var regex = new RegExp(/1([^1]*)1/g);
var numOnes = new RegExp(/[^1]/g);



do {

        // filter odd or even chairs
        chairs = chairs.replace(regex, mask[last%2]);
        last = odd;
// while there are still chairs
} while ((odd = chairs.replace(numOnes, '').length) > 1)

// return the answer
chairs.indexOf('1')+1;
pending…
maths
    var totalLength = 100;
    var numIterations = Math.round(Math.log(totalLength) / Math.LN2);
    var startingPoint = 2;
    var currentLength = totalLength;

    for(var count = 1; count < numIterations; count++) {
            startingPoint += ((currentLength + 1) & 1) << count;
            currentLength = (totalLength - startingPoint) >> count;
    }

    // answer
    startingPoint;
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