Convert array-like

JavaScript performance comparison

Test case created by Cezary Tomczyk

Preparation code

<script>
  var convert1 = function(o) {
      try {
        //this converts object into an array when browsers support it
        return Array.prototype.slice.call(o, 0);
      } catch (ex) {
        var i = 0,
            len = o.length,
            result = [];
        result.length = len;

        while (i < len) {
          result[i] = o[i];
          i++;
        }
        return result;
      }
      };      
      
  var convert2 = function(o) {
      var x = o.length,
          q = [];

      while (x--) {
        q.push(o[x]);
      }

      return q.reverse();
  };
      
      
      
  var convert3 = function(o) {
      return Array.prototype.slice.call(o, 0);
      };
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var elms = document.links;

};
</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 slice try-catch
convert1(elms);
pending…
Array slice no try-catch
convert3(elms);
pending…
Only loop
convert2(elms);
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