simulate range of python

JavaScript performance comparison

Revision 2 of this test case created by kerryChen95

Preparation code

<script>
// Generate an integer Array containing an arithmetic progression. A port of
  // the native Python `range()` function. See
  // [the Python documentation](http://docs.python.org/library/functions.html#range).
  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>
<script>
function arrayApplyAndMap (start, end, step) {
  if (arguments.length <= 1) {
    end = start || 0;
    start = 0;
  }
  step = step || 1;
  return Array.apply(null, new Array( Math.max(Math.ceil( (end - start) / step ), 0) )).map(function (val, i) { return i * step + start; });
};

var underscore1_4_4 = _.range;

var longLengthSmallStep = [1, 10000, 1];
var longLengthLargeStep = [1, 10000, 1000];
var shortLength = [1, 5];
</script>

Preparation code output

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 apply and map
arrayApplyAndMap.apply(null, longLengthSmallStep);
arrayApplyAndMap.apply(null, longLengthLargeStep);
arrayApplyAndMap.apply(null, shortLength);
pending…
underscore1.4.4
underscore1_4_4.apply(null, longLengthSmallStep);
underscore1_4_4.apply(null, longLengthLargeStep);
underscore1_4_4.apply(null, shortLength);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment