Pushing to Arrays

JavaScript performance comparison

Test case created by SarathSantoshDamaraju

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
  
  function update_with_array_push() {
    list.push(21);
  }
  
  function update_with_array_length() {
    list[list.length] = 21;
  }
  
  function update_with_array_concat() {
    list = list.concat([21]);
  }
  
  function small_array_push() {
    let arr = [];
    
    for (let i = 0; i < 1000; i++)
    {
        arr.push(i);
    }
  }
  
  function small_array_length() {
    let arr = [];
    
    for (let i = 0; i < 1000; i++)
    {
        arr[arr.length] = i;
    }
  }
  
  function small_array_concat() {
    let arr = [];
    
    for (let i = 0; i < 1000; i++)
    {
        arr = arr.concat([i]);
    }
  }
  
  function large_array_push() {
    let arr = [];
    
    for (let i = 0; i < 10000; i++)
    {
        arr.push(i);
    }
  }
  
  function large_array_length() {
    let arr = [];
    
    for (let i = 0; i < 10000; i++)
    {
        arr[arr.length] = i;
    }
  }
  
  function large_array_concat() {
    let arr = [];
    
    for (let i = 0; i < 10000; i++)
    {
         arr = arr.concat([i]);
    }
  }

};
</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.length ~ 10k. With Push
large_array_push()
pending…
Array.length ~ 10k. With length
large_array_length()
pending…
Array.length ~ 10k. With Push
large_array_concat()
pending…
Array.length ~ 1k. With Length
small_array_length()
pending…
Array.length ~ 1k. With Length
small_array_push()
pending…
Array.length ~ 1k. With Concat
small_array_concat()
pending…
Small Array with Push
update_with_array_push()
pending…
Small Array with Length
update_with_array_length()
pending…
Small Array with Concat
update_with_array_concat()
pending…

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

0 Comments