Re: Object.observe()able properties on the web platform

On 8/20/14, 3:03 PM, Domenic Denicola wrote:
>> How would you implement observable properties efficiently?
> The efficiency is already solved by the Object.observe implementation. My understanding is that the overhead is close to negligible for cases when nobody observes the object.

The overhead of checking whether something observes the object might be 
quite non-negligible in itself.  Especially so for cases where the 
property value is computed lazily, because then you need some sort of 
extra something to notice when the value might have changed, which you 
don't need without Object.observe.

> If you are asking how you tap into that as a developer, you do something like `this._notifier = Object.getNotifier(this)` in the constructor, and then do `this._notifier.notify({ type: 'update', name: 'propertyName', oldValue: propertyOldValue })` inside the relevant setter.

You just assumed you have the old and new value.

You also constructed an object.  And defined properties on it.  I can 
assure you that doing that is non-negligible in performance terms.

> Obviously real implementations would not use underscored-properties, but you can see how it is very much automatable via a WebIDL binding layer.

It's not entirely, because the bindings layer has no idea when property 
values change, except for the obvious case of writable properties that 
can't change out-of-band.


Received on Wednesday, 20 August 2014 19:16:27 UTC