admin管理员组文章数量:1417691
The MDN hints that using .setPrototypeOf()
will have a bad influence on the future performance of your code.
I also read a few Questions about why changing the [[Prototype]] of an object will lower the performance. But none of the answers really explained whats going on in the background. So I wonder if this also applies for new Objects.
In particular I really like to do things like this:
var MyPrototype = {
method1 : function(){...},
method2 : function(){...},
...
};
var newObject = Object.setPrototypeOf({
property : 1,
property2 : 'text'
}, MyPrototype);
Unfortunately you can't do this with Object.create
since it doesn't accept a plain object literal.
Does my use of setPrototypeOf
also decrease the performance of the executing JS engine?
The MDN hints that using .setPrototypeOf()
will have a bad influence on the future performance of your code.
I also read a few Questions about why changing the [[Prototype]] of an object will lower the performance. But none of the answers really explained whats going on in the background. So I wonder if this also applies for new Objects.
In particular I really like to do things like this:
var MyPrototype = {
method1 : function(){...},
method2 : function(){...},
...
};
var newObject = Object.setPrototypeOf({
property : 1,
property2 : 'text'
}, MyPrototype);
Unfortunately you can't do this with Object.create
since it doesn't accept a plain object literal.
Does my use of setPrototypeOf
also decrease the performance of the executing JS engine?
- var newObject = Object.create(MyPrototype, {"property1":{value:1},"property2":{value:'text'}}); – Data Commented Sep 7, 2015 at 19:02
-
1
I know that but I think the
Object.create()
properties object is too heavy for most cases. I'd prefer to just write the usual object literal. You can avoid a lot of plexity without the additional sub- objects with thevalue
property. – Jovan Commented Sep 7, 2015 at 19:09 - So you are familiar with Why is mutating the [[prototype]] of an object bad for performance?? – Bergi Commented Sep 7, 2015 at 19:14
-
I totally agree, Object.create() is way too messy when adding props, especially when you define writable, enumerable, etc for each one. I really wanted to use Object.create() but have now decided to use the function constructor pattern with
new
, it's much easier. – Data Commented Sep 7, 2015 at 19:14 -
3
The marked answer doesn't really answer the question, only provides a solution in the case that
setPrototypeOf()
really is (or will have) a large negative performance impact. But there's nothing saying that it actually does have a negative performance impact. The Firefox JavaScript console, too, gives the same warning. Why is that? – user5670895 Commented Oct 30, 2015 at 15:11
2 Answers
Reset to default 6If you fear (as apparently you should..) the performance impact of using Object.setPrototypeOf()
, but want to keep your object creation syntax similar to how your code is structured, try this:
var MyPrototype = {
method1 : function(){...},
method2 : function(){...},
...
};
var newObject = Object.assign(Object.create(MyPrototype), {
property : 1,
property2 : 'text'
});
Another option is to merge the object literal with a shallow copy of myPrototype
, although this may not be your desire.
var MyPrototype = {
method1 : function(){},
method2 : function(){}
};
var newObject = Object.assign({
property : 1,
property2 : 'text'
}, MyPrototype);
本文标签: javascriptWhats the performance impact of setPrototypeOf on a new ObjectStack Overflow
版权声明:本文标题:javascript - Whats the performance impact of setPrototypeOf on a new Object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745276095a2651196.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论