Creating Javascript Arrays: new vs []

My last post quickly compared the performance of creating javsacript objects via new object() and {}.

So for curiousity’s sake let’s do the same for arrays.

We’ll compare these two methods for instantiating arrays:

1
2
3
4
5
  // Method 1: Using new operator
  var arr1 = new Array();

  // Method 2: Using literal syntax
  var arr2 = [];

To no suprise the literal syntax is almost twice as fast.

Unless you need to set the length when creating the array we should favour the the literal syntax of var a = [] instead of the verbose var a = new Array() syntax for performance and readability.

Comments