Cloning Arrays (v26)

Revision 26 of this benchmark created by Steven Goris on


Preparation HTML

<script>
  var a = [];
  for (var i = 0; i < 2000; i++) {
   a[i] = i;
  }
</script>

Setup

var __read = (this && this.__read) || function (o, n) {
      var m = typeof Symbol === "function" && o[Symbol.iterator];
      if (!m) return o;
      var i = m.call(o), r, ar = [], e;
      try {
          while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
      }
      catch (error) { e = { error: error }; }
      finally {
          try {
              if (r && !r.done && (m = i["return"])) m.call(i);
          }
          finally { if (e) throw e.error; }
      }
      return ar;
  };
  var __spread = (this && this.__spread) || function () {
      for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
      return ar;
  };

Test runner

Ready to run.

Testing in
TestOps/sec
Typescript downleveliteration
var b = __spread(a);
ready
Spread operator
var b = [...a];
ready
Array from
var b = Array.from(a);
ready
Concat
var b = a.concat();
ready
Slice
var b = a.slice();
ready
Map arrow function
var b = a.map(e => e);
ready
Slice with 0 index
var b = a.slice(0);
ready
Concat 2
var b = [].concat(a);
ready
Map
var b = a.map(function(e) {
    return (e);
});
ready
Slice with 0 index and length
var b = a.slice(0, a.length);
ready

Revisions

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