The following drops the need for the closure, and lets v8 optimize nicely.
Unfortunately you lose the hidden aspect of the internal state.
function Machine(power) {
this.enabled = true;
}
Machine.prototype.enable = function() {
this.enabled = true;
};
Machine.prototype.disable() {
this.enabled = false
};
On Tue, Jun 10, 2014 at 4:31 PM, Ilya Kantor <[email protected]> wrote:
> Hi Ben,
>
> Is that somehow related with "Hidden classes" for objects or that's a
> completely another optimization technique?
>
> среда, 11 июня 2014 г., 0:29:07 UTC+4 пользователь Ben Noordhuis написал:
>
>> On Tue, Jun 10, 2014 at 9:58 PM, Ilya Kantor <[email protected]> wrote:
>> > Hi,
>> >
>> > When I create many objects new Machine like this
>> > ```
>> > function Machine(power) {
>> > var enabled = false;
>> >
>> > this.enable = function() {
>> > enabled = true;
>> > };
>> > this.disable = function() {
>> > enabled = false;
>> > };
>> > }
>> >
>> > var machines = []
>> > for(var i=0; i<10000; i++) machines.push(new Machine)
>> > ```
>> >
>> > ...I see in Chrome Heap Profile that every object has 36 bytes for
>> function
>> > enable/disable.
>> > That is so even if the function code is actually longer.
>> >
>> > Do I get it right that V8 actually creates these functions only ONE
>> time and
>> > uses it for all machines?
>>
>> That's correct, there is only one function. Those 36 byte instances
>> that you're seeing are closure environments for the captured variable.
>>
> --
> --
> v8-users mailing list
> [email protected]
> http://groups.google.com/group/v8-users
> ---
> You received this message because you are subscribed to the Google Groups
> "v8-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.