What’s the difference between “Array()” and “[]” while declaring a JavaScript array?

后端 未结 18 1686
生来不讨喜
生来不讨喜 2020-11-21 12:00

What\'s the real difference between declaring an array like this:

var myArray = new Array();

and

var myArray = [];
<         


        
18条回答
  •  失恋的感觉
    2020-11-21 12:57

    The difference of using

    var arr = new Array(size);
    

    Or

    arr = [];
    arr.length = size;
    

    As been discussed enough in this question.

    I would like to add the speed issue - the current fastest way, on google chrome is the second one.

    But pay attention, these things tend to change a lot with updates. Also the run time will differ between different browsers.

    For example - the 2nd option that i mentioned, runs at 2 million [ops/second] on chrome, but if you'd try it on mozilla dev. you'd get a surprisingly higher rate of 23 million.

    Anyway, I'd suggest you check it out, every once in a while, on different browsers (and machines), using site as such

提交回复
热议问题