javascript - Is there a performance advantage in using an object literal over a self instantiated constructor? -
question
is there performance advantage in using object literal on self instantiated constructor?
examples
object literal:
var foo = { //... };
self instantiated constructor:
var foo = new function () { //... };
yes (the object literal will faster), subtly different in implementation1 , represent different goals. constructor form "has bunch more stuff" while literal form can more highly optimized - definition (of as-of-yet-fixed set of properties), , not sequence of statements.
even though micro-benchmark (which interesting, arun!) might show 1 being "much slower", doesn't matter in real program2 amount of relative time spent in either construct approaches nothing.
1 when constructor used a prototype must introduced. not case object literal due it's fixed chain behavior.
every object created constructor has implicit reference (called object’s prototype) value of constructor’s “prototype” property.
other overhead work includes creating new execution context, copying on additional properties, , checking return value. (in posted case has create new one-off function object before can use constructor adds additional overhead).
2 i'm sure there counter-examples. such cases, can hope problem has been thoroughly benchmarked other bottlenecks identified , removed.
Comments
Post a Comment