fromArray

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

<script>
function toArray(xs) {
  var out = [];
  while (xs[0] === "Cons") {
    out.push(xs[1]);
    xs = xs[2];
  }
  return out;
}

function fromArray(arr) {
  var out = ["Nil"];
  for (var i = arr.length; i--;) {
    out = ["Cons", arr[i], out];
  }
  return out;
}

function toArrayS(xs) {
  'use strict';
  var out = [];
  while (xs[0] === "Cons") {
    out.push(xs[1]);
    xs = xs[2];
  }
  return out;
}

function fromArrayS(arr) {
  'use strict';
  var out = ["Nil"];
  for (var i = arr.length; i--;) {
    out = ["Cons", arr[i], out];
  }
  return out;
}


function mkList(n) {
  var out = ["Nil"];
  for (var i = n; i--;) {
    out = ["Cons", i, out];
  }
  return out;
}

var list = mkList(1000);
var arr = []
for (var i = 1000; i--; ) { arr.push(i);}
</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
toArray
toArray(list)
pending…
strict toArray
toArrayS(list)
pending…
fromArray
fromArray(arr);
pending…
strict fromArray
fromArrayS(arr);
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