Reputation:
Is this:
function Con () {
var foo = 42;
this.foo = foo
}
more slow than this:
function Con () {
this.foo = 42;
}
On subsequent uses of the foo variable on an object returned by an invocation of the 'Con'-constructor? Ie. does it hurt if the variable has first been living in the closure of the constructor invocation?
EDIT: @FakeRainBrigand: Well, here you measure the time of constructing an object using the constructor. Right? I was rather thinking about subsequent uses of a constructed object, like this:
var myObj = new Con();
var bar = myObj.foo;
What is the time usage of the last line only (that is, the accessing of the variable foo
) in the two cases?
Upvotes: 0
Views: 65
Reputation: 86270
It's slightly slower. Here's a performance test. It's more or less negligible, and there are nearly definitely other issues you should worry about. We can do this 60 million times per second, so worry about the things you can only do a few times per second.
I changed the tests to not include actually making the constructor, and they became even more identical. This is likely due to internal optimizations. Here, the two fall into the margin of error.
Upvotes: 2