Understood. This message just caught my attention, as I am often sad that state hidden by closure comes with a relatively high price compared to the state existing on "this".
Anyways, my tests also indicate the function bodies are correctly shared, leaving only the captured context duplicated. It would be great to have someone with better internals knowledge tell share us the internal details. On Tue, Jun 10, 2014 at 5:24 PM, Ilya Kantor <ilia...@gmail.com> wrote: > Hi Stefan, > > I'm asking to deeper understand V8. > > What's going on in the original code (see first message) which leads to > single-function-per-many objects? > > >> 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 <ili...@gmail.com> 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 <ili...@gmail.com> 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 >>> v8-u...@googlegroups.com >>> >>> 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 v8-users+u...@googlegroups.com. >>> >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- > -- > v8-users mailing list > v8-users@googlegroups.com > 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 v8-users+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- -- v8-users mailing list v8-users@googlegroups.com 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 v8-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.