Friday, August 10, 2007

Don't use "new Array()"

Check out these two lines of code:


var foo = new Array();
var bar = [];


They do the same thing. So, save yourself 9 characters, and use [].

1 comments:

said...

Not to mention that literals are always faster than object constructors.

Observe (blame Blogger's lack of proper tag allowance for the ugly code):


.ie measure(null, [], 10000, function() { var x = new Array(); x[1] = 0; }, function() { var x = []; x[1] = 0; });
IE7: func 1: [T:701ms], func 2: [T:421ms]

.ff measure(null, [], 10000, function() { var x = new Array(); x[1] = 0; }, function() { var x = []; x[1] = 0; });
FF2: func 1: [T:1011ms], func 2: [T:731ms]