0

A bit of background: I've been implementing a library for 2 way data binding for my own enjoyment and learning purposes and I've been weeding out performance issues one after another. This is how I noticed the big warning at the top of the MDN documentations for Object.setPrototypeOf which states

Warning: Changing the [[Prototype]] of an object is, by the nature of how modern JavaScript engines optimize property accesses, a very slow operation, in every browser and JavaScript engine. The effects on performance of altering inheritance are subtle and far-flung, and are not limited to simply the time spent in the Object.setPrototypeOf(...) statement, but may extend to any code that has access to any object whose [[Prototype]] has been altered. If you care about performance you should avoid setting the [[Prototype]] of an object. Instead, create a new object with the desired [[Prototype]] using Object.create().

This is very curious to me as to why this would be the case.

PS: for anyone who's curious at why i'm extensively using Object.setPrototypeOf here's the source code for that part of the project https://github.com/muggy8/proxymity/blob/master/src/data-proto.js

Muggy Ate
  • 333
  • 1
  • 3
  • 12

1 Answers1

0

Because changing the prototype of an object clear the browser is caching of how he can access its properties and the browser will have to make lookup to access the new prototype but i think it will not be a great difference of creating new one or adding properties to the object because it will do the lookup. You can see kyle simposon answer about changing the prototype. kyle simpson prototype

Tarek Essam
  • 3,602
  • 2
  • 12
  • 21