15

Many people say that you should avoid new Object, new Array()and instead use {}. [], and true/false.

What are the benefits of using the literal constructs to get a new instance of an Object or Array rather than using new? I konw that Crockford doesn't like new but is that the main argument?

elasticrat
  • 7,060
  • 5
  • 36
  • 36

5 Answers5

8

The advantages of object and array literals over using the respective constructors are:

  • Shorter and more readable
  • Safer: literals will still work when the Array or Object constructors have been overridden
  • Possibly faster, though it's unlikely to be a major consideration (any bottlenecks will almost certainly originate elsewhere in code)

In the case of arrays, there's an additional advantage of a literal: it's impossible to create an array with a single member using the Array constructor alone. For example, [3] will create an array with one element which is the number 3, while new Array(3) creates an array of length 3.

Update: the following paragraph is no longer relevant now the question has been edited.

Regarding Booleans, you seem to have a misconception: new Boolean(false) is not the same as false. The Boolean() constructor creates a Boolean object whereas false and true are Boolean primitives. In fact, new Boolean(false) evaluates to true when coerced into a Boolean in, for example, an if statement. In short, there's very rarely a reason to use the Boolean() constructor. Use true and false instead. Similarly, other primitives such as strings and numbers have corresponding String() and Number() constructors that produce String and Number objects that are different to primitive strings and numbers and should generally be avoided.

Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • Edited the original questions to remove the incorrect true/false instead of new Boolean() statement. – elasticrat Nov 27 '10 at 16:05
  • 2
    @Paul: I'll leave the Boolean stuff in my answer: it's still useful information. – Tim Down Nov 27 '10 at 16:06
  • Some interesting performance notes: Using `new Object()` and `new Array()` seems to actually be faster in newer browsers than declaring with literals like `{}` and `[]`. (see [this](http://jsperf.com/array-creation-blank), [this](http://jsperf.com/array-creation-perf), and [this](http://jsperf.com/object-performance)). – Chad Jan 23 '12 at 14:10
  • One the main reasons not to use `new Array()` is because of the ambiguity when passing a single argument. The reasons you mentioned are valid but aren't major problems – Ruan Mendes Sep 18 '15 at 12:00
  • @JuanMendes Can you please explain your comment with an example? – Ofir Hadad Jan 31 '16 at 07:43
  • @Chad Actually I found all 3 test are faster for the literals. (Using Chrome v47) – Ofir Hadad Jan 31 '16 at 07:44
  • @Ofear, `new Array(1,2,3)` retuns `[1,2,3]` but `new Array(1)` returns `[undefined]`; – Ruan Mendes Jan 31 '16 at 15:54
  • @Ofear Your comparing performance in your modern browser vs a post I did 2 years ago? I'm sure things have changed. – Chad Jan 31 '16 at 18:23
  • hehe, I didn't look on the date it was posted. :) – Ofir Hadad Feb 03 '16 at 09:20
  • any idea how to modify array literals constructor? – assayag.org Jun 17 '20 at 20:47
4

For example, if you want to do this:

{name:"bla",address:"address"}

the new Object() way would be:

var o = new Object();
o.name="bla";
o.address="address";

The first one is much shorter. And I think that it would be faster in many browsers, too (jsperf testcase).

thejh
  • 44,854
  • 16
  • 96
  • 107
  • Faster, yes. Several years ago I tried the `new Array()` method then added all the values with `arr.push()` - there were thousands of values to add (I had no life =_= ) and it was slow as... fudge. Just putting all the values insie the array constructor `new Array(val1,val2,val3...)` was hugely faster! – Niet the Dark Absol Nov 27 '10 at 14:27
  • 1
    @Kolink - That's likely because you were calling `.push()`. The question here is more about constructor `new Array(val1,val2,val3...)` vs literal `[val1,val2,val3...]` – user113716 Nov 27 '10 at 14:31
  • `new Object()` and `new Array()` are a lot faster when you need to construct a ton of them and then set/append data very often. In a parser/decoder there's a noticeable speed gain when using them. – Ivo Wetzel Nov 27 '10 at 15:16
3

I think it's mostly about succinctness.

Why write new Array() when you can write [], or new Object() when you can write {}?

Also new Boolean() is entirely redundant. A boolean is always going to need to be either true or false, so you should definitely use the built in constants for that.

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
  • `new Boolean(true)` is worse than redundant, it's different to the primitive `true`. See my answer. – Tim Down Nov 27 '10 at 16:01
1

In most cases an object literal or array literal is sufficient and easier to use. You can even call or apply prototype methods (e.g. [].prototype.slice.call(someObj)) It's mostly faster too. You'll may want to notice that {} and []‘s constructor cannot be overwritten and that Object and Array are constructors where {} and [] are instances.

Community
  • 1
  • 1
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • I've run the jsperf test you link to and the literals are the slowest of the three (_on my machine_). I'm guessing that's down to Firefox 3.6.12 – Matt Ellen Nov 27 '10 at 14:41
  • I ran the tests in Chrome, where using literals was faster. If you scroll down on the jsperf page there are some extra results from 'browserscope' – KooiInc Nov 27 '10 at 14:50
0

One reason not yet mentioned is the ambiguity when passing parameters to the Array constructor. It's all specified behavior, it's just quirky.

new Array(1,2,3); // [1,2,3], that's OK
new Array(1); // [undefined], some may expect to get [1] instead

A JavaScript array is initialized with the given elements, except in the case where a single argument is passed to the Array constructor and that argument is a number. (See below.) Note that this special case only applies to JavaScript arrays created with the Array constructor, not array literals created with the bracket syntax. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Syntax

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217