In reading this http://www.html5rocks.com/en/tutorials/speed/v8/ it makes sense that changing the type of variable at runtime forces browsers to work harder than when keeping them consistent.
So does that mean this is not a good idea:
var x = { alpha: null, bravo: null, charlie: null, delta: null, echo: null } x.alpha = {a:1, b:true} x.bravo = 13 x.charlie = true x.delta = [1,2,3] x.echo = 'abc' Because here the types started as null, then got changed to object, int, boolean arrary.
And for the sake of simplicity, afterwards these types never change.
Or is this more efficient:
var x = { alpha: {}, bravo: 0, charlie: false, delta: [], echo: '' } x.alpha = {a:1, b:true} x.bravo = 13 x.charlie = true x.delta = [1,2,3] x.echo = 'abc' I can understand changing types from say a number to an array is not a good idea. What about changing from null into a type once during execution?
The books and blogs I have read mostly say to define variables with null (as opposed to undefined) when the values are only known at runtime. At face value, this seems wrong, as defining with their empty type avoids a type change.
xas an empty object and only add certain properties if they have a real value. Then you can check whether a property is set or not using.hasOwnProperty()for instance.