Inserting an item into the middle of an array

JavaScript performance comparison

Test case created by kev

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arr = ['one', 'two', 'three', 'four'];
  
  function splice() {
    arr.splice(arr.length / 2, 0, 'hello');
    return arr;
  }
  
  function concat() {
    var firstHalf = arr.slice(0, arr.length / 2);
    var secondHalf = arr.slice(arr.length / 2);
    return firstHalf.concat('hello', secondHalf);
  }
  
  function rest() {
    return [
      ...arr.slice(0, arr.length / 2),
      'hello',
      ...arr.slice(arr.length / 2)
    ];
  }

};

Benchmark.prototype.teardown = function() {
  var arr = ['one', 'two', 'three', 'four'];

};
</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
splice method
splice();
pending…
concat method
concat();
pending…
rest method
rest();
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