Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Object.observe() affect performance?

The Object.observe() JavaScript API allows any piece of code to receive change notifications for all property changes of any JavaScript object.

Doesn't this severely affect the code generation and performance optimizations that can be performed by the JavaScript Engine (i.e. V8)? It seems like the generated native code now has to check for every single write to the object if a change notification must be generated. It is not possible to statically determine if a given object has notifications set up or not. So the checks cannot be optimized out.

It seems like any conforming JavaScript engine is now locked in to a permanent and severe loss in performance due to this API.

like image 378
usr Avatar asked Feb 27 '13 14:02

usr


People also ask

What is object observe?

Object. observe , a.k.a. O.o , is a feature to be added to JavaScript as part of ECMAScript 7 to support object change detection natively in the browser. Although ES7 is not completed yet, this feature is already supported in Blink-based browsers (Chrome and Opera).

What is observer object in JavaScript?

The Observer pattern offers a subscription model in which objects subscribe to an event and get notified when the event occurs. This pattern is the cornerstone of event driven programming, including JavaScript. The Observer pattern facilitates good object-oriented design and promotes loose coupling.

Are objects slower than arrays?

Objects will be used when we need fast access, insertion and removal of an element as objects are comparatively much faster than the arrays in case of insertion and removal.

Are objects faster than arrays?

The short version: Arrays are mostly faster than objects.


2 Answers

Modern JavaScript engines utilize inline caching and adaptive recompilation techniques to minimize impact of the dynamic dispatch on the generated code.

If we are speaking about V8 then the fact whether object is observed or not is encoded in its hidden class. Both inline caches stubs and optimized code already check hidden class against some expected value to determine whether an object has an expected shape or not. The very same check gives information about the fact whether the object is observed or not. So nothing changes on the code paths that work with non-observed objects. Starting to observe the object is treated the same way as changing it shape: object's hidden class is switched to a different one, with an observed bit set: you can read Runtime_SetIsObserved to see this.

Similar reasoning applies to the parts of the system that omit guards in the optimized code and instead deoptimize code dependant on "shape" assumptions: once an object becomes observed all optimized code depending on the assumption that such object was not observed will be deoptimized. Thus again no price is paid for unobserved objects.

That said, current implementation of Object.observe in V8 makes observed objects pay a high price because it normalizes them (turns them into dictionary representation) and requires round trips through runtime system for observation recording. But there are no inherent technical difficulties in significantly reducing this cost later.

like image 155
Vyacheslav Egorov Avatar answered Oct 19 '22 23:10

Vyacheslav Egorov


Doesn't this severely affect the code generation and performance optimizations that can be performed by the JavaScript Engine (i.e. V8)?

Yes. Just the same as Proxies, Getters/Setters and maybe even prototype objects - all of them are dynamic in JavaScript.

However, due to their asynchronity new (and better) optimisations will be possible; and they could make other, more inefficient code obsolete. Citing the Goals from the harmony draft:

  • No wrapper or proxy objects needed, providing memory efficiency and object identity
  • Change notifications on add/delete of a property on an object
  • Change notifications on modifications to property descriptor of properties on an object
  • The ability for an object to manually indicate when an accessor property has changed
  • Efficiently implementable in engines
  • Simple, targeted, extension to current ES
  • Asynchronous notification of changes, but allow synchronous fetching of changes pending delivery
like image 26
Bergi Avatar answered Oct 20 '22 01:10

Bergi