array literal vs new array range

JavaScript performance comparison

Test case created

Preparation code

var range = function(start, stop, step) {
    if (arguments.length <= 1) {
     stop = start || 0;
     start = 0;
   }
   step = arguments[2] || 1;

   var len = Math.max(Math.ceil((stop - start) / step), 0);
   var idx = 0;
   var range = new Array(len);

   while(idx < len) {
     range[idx++] = start;
     start += step;
   }

   return range;
 };
<script>
Benchmark.prototype.setup = function() {
    var rangeLiteral = function(start, stop, step) {
        if (arguments.length <= 1) {
          stop = start || 0;
          start = 0;
        }
        step = arguments[2] || 1;
   
        var len = Math.max(Math.ceil((stop - start) / step), 0);
        var idx = 0;
        var range = [];
   
        while(idx < len) {
          range[idx++] = start;
          start += step;
        }
   
        return range;
      };
};
</script>

Preparation code output

var range = function(start, stop, step) { if (arguments.length <= 1) { stop = start || 0; start = 0; } step = arguments[2] || 1; var len = Math.max(Math.ceil((stop - start) / step), 0); var idx = 0; var range = new Array(len); while(idx < len) { range[idx++] = start; start += step; } return range; };

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Array new
range(10000);
pending…
Arra literal
rangeLiteral(10000);
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