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?

asked Nov 27, 2010 at 14:14
1
  • 3
    new Boolean(true) is not equivalent to true Commented Nov 27, 2010 at 14:35

5 Answers 5

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.

answered Nov 27, 2010 at 15:57
Sign up to request clarification or add additional context in comments.

10 Comments

Edited the original questions to remove the incorrect true/false instead of new Boolean() statement.
@Paul: I'll leave the Boolean stuff in my answer: it's still useful information.
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, this, and this).
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
@JuanMendes Can you please explain your comment with an example?
|
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).

answered Nov 27, 2010 at 14:18

3 Comments

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!
@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...]
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.
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.

answered Nov 27, 2010 at 14:18

1 Comment

new Boolean(true) is worse than redundant, it's different to the primitive true. See my answer.
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.

answered Nov 27, 2010 at 14:38

2 Comments

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
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'
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

answered Jan 31, 2016 at 19:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.