2 ms·
I always think it's weird when I see people using "new" to make Array / Object. Unless you're using the ridiculously rare pre-initialization of an Array via "ne
by kemayo 16y ago
I always think it's weird when I see people using "new" to make Array / Object. Unless you're using the ridiculously rare pre-initialization of an Array via "new Array(5)" it just feels clunky.
- gruseom 16y agoPre-allocating an array using new is measurably better for performance in some browsers. This sometimes matters.
- M1573RMU74710N 16y ago> Pre-allocating an array using new is measurably better for performance in some browsers. If you need to create a new array and populate it with values, then using new Array( size ), and then looping through it to populate it will be faster than using a literal or new Array() and then looping through it. It should be noted though, that the difference is negligible unless you're working on creating and pre-populating a lot of very large arrays. This situation is rare to begin with, and even then in most cases it's not a big difference. In perhaps the most common case, creating an empty array, a literal is generally faster than (or equivalent with) using the Array constructor (at least, in tests I have done). I would go with the syntax you believe is cleaner unless you have a demonstrable perf bottleneck....speed shouldn't even be brought up until then (unless you are trying to decide between two truly equal options, then picking the faster one is reasonable, but that's not the case here)