Is it "okay" to use v8::Object::SetPrototype? Scary warnings about using Object.setPrototypeOf()..

416 views
Skip to first unread message

Zac Hansen

unread,
Feb 11, 2016, 11:44:24 PM2/11/16
to v8-users
I want to do the equivalent of Object.create() in javascript from C++, but I don't see any way to specify the prototype of a new v8::Object from the API.

I was planning on creating the object with v8::Object::New and then calling v8::Object::SetPrototype() on it, until I saw this:


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 obj.__proto__ = ... 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().



That's a pretty terrifying situation, so I want to make sure that using SetPrototype() doesn't incur that kind of penalty - at least on a newly created and unused object.

Thank you.

--Zac

Jakob Kummerow

unread,
Feb 12, 2016, 4:35:18 AM2/12/16
to v8-users
Whether an object's prototype is changed from JavaScript or from C++ doesn't make a difference. (Why would it?)

The preferred way to set a newly allocated object's prototype is to use the FunctionTemplate's PrototypeTemplate. See the large comment for class FunctionTemplate in v8.h.


--
--
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.

Zac Hansen

unread,
Feb 12, 2016, 4:47:18 AM2/12/16
to v8-u...@googlegroups.com

When you say:

The preferred way to set a newly allocated object's prototype is to use the FunctionTemplate's PrototypeTemplate. See the large comment for class FunctionTemplate in v8.h.


What does "preferred" mean?  Is it because of the type of thing in the warning I put in my original post?  Or  some other reason why that method is "preferred"?

--Zac


You received this message because you are subscribed to a topic in the Google Groups "v8-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/v8-users/L9e3eHyjE4A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.

Jakob Kummerow

unread,
Feb 12, 2016, 5:41:36 AM2/12/16
to v8-users
It avoids having to change the prototype.

(That warning on MDN is awesome, strong +1 to that. Being able to modify prototypes at all is a mis-feature IMHO.)

Zac Hansen

unread,
Feb 12, 2016, 5:45:45 AM2/12/16
to v8-u...@googlegroups.com
Ok, so there is no ongoing performance penalty if I change the prototype of an object?

Is there a reason there's no API call for setting the prototype of a javascript object created in C++ to a javascript-created object?  In other words, why does v8::Object::New() not have an option to take a v8::Object?

Jakob Kummerow

unread,
Feb 12, 2016, 5:59:25 AM2/12/16
to v8-users
On Fri, Feb 12, 2016 at 11:45 AM, Zac Hansen <xax...@gmail.com> wrote:
Ok, so there is no ongoing performance penalty if I change the prototype of an object?

Who said that?
 
Is there a reason there's no API call for setting the prototype of a javascript object created in C++ to a javascript-created object?  In other words, why does v8::Object::New() not have an option to take a v8::Object?

Sounds like you *want* to muck with dynamically created/modified prototype chains. That'll probably end up being slow either way. Maybe you don't care. So maybe just do whatever seems easiest to you, and see if it works out OK?

Zac Hansen

unread,
Feb 12, 2016, 6:12:24 AM2/12/16
to v8-u...@googlegroups.com
If I wanted to re-implement Object.create(prototype, properties_object) using the API with the same performance characteristics as the built-in Object.create(), what API calls would I use in the callback from my FunctionTemplate?


Reply all
Reply to author
Forward
0 new messages