VaR arr = [] and VAR arr = new array ();

Source: Internet
Author: User

Use of new keywords
In addition to instantiating an object or a rare case where data needs to be loaded at a time delay, you basically do not need to use the new keyword. Allocating a large number of new variable addresses in Javascript is a very slow operation. For efficiency, you should always use object symbols.

New array () Will instantiate an object variable, and VAR arr = [], so it directly declares a variable. Obviously, the performance loss of an instance object is greater than that of an object directly declared.

The only difference in syntax between [] and new array () is that new array () can directly set the length of the array.
For example, new array (100)
However, this feature often brings little trouble to program modification.
For example, new array (56,46, 78) sets three elements for the array at the beginning, and then removes the two elements for modifying the program for a certain reason. If you don't pay attention to it, it becomes new array (56 ). Cause program errors.


Arr = [] when creating an object, it will allocate the default number of elements, that is, even if you only put one element in it, it will occupy the space of the default number of elements, when the number of elements exceeds the limit,
New space will be allocated again.
This is generally used when the number of elements is not determined. In theory, it is suspected of wasting memory space (it will occupy the vast majority if it is not used completely ).

New array ()
When creating an object, the number of elements has been determined. When the number of elements is known, the number of elements is more limited, but more "energy-saving ".

VaR arr = [] and VAR arr = new array ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.