erato

JavaScript performance comparison

Test case created by Carl Lumma

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var eratosthenes1 = function(n) {
    var mask = new Array(n);
    var limit = Math.sqrt(n);
    var result = [];
    for (var i = 2; i <= limit; i++) {
      if (!mask[i]) {
        for (var j = i * i; j <= n; j += i) {
          mask[j] = true;
        }
      }
    }
    for (i = 2; i <= n; i++) {
      mask[i] || result.push(i);
    }
    return result;
  }
  var eratosthenes2 = n =>
  	Array(Math.floor(Math.sqrt(n)) - 1).
  		fill().map((_v, i) => i + 2).reduce((mask, v, i) =>
  			(mask[v] && (Array(Math.ceil(n === v * v ? 1 : (n - (v * v)) / v)).
  				fill().forEach((_vv, ii) =>
  					mask[(v * v) + (v * (ii))] = false)), mask), Array(n).fill(true)).
  						map((v, i) => v && i).filter((i) => i !== false).slice(2);
  

};
</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
loopz
eratosthenes1(5000)
pending…
straight
eratosthenes2(5000)
pending…

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

0 Comments