为什么改变对象的 [[prototype]] 对性能不利?

IT技术 javascript performance prototype prototype-chain
2021-01-28 18:55:34

来自标准 setPrototypeOf功能 以及非标准属性的 MDN 文档 __proto__

强烈建议不要改变对象的 [[Prototype]],无论这是如何实现的,因为它非常慢,并且不可避免地会减慢现代 JavaScript 实现的后续执行速度。

使用Function.prototype添加属性是向 javascript 类添加成员函数方式。然后如下图所示:

function Foo(){}
function bar(){}

var foo = new Foo();

// This is bad: 
//foo.__proto__.bar = bar;

// But this is okay
Foo.prototype.bar = bar;

// Both cause this to be true: 
console.log(foo.__proto__.bar == bar); // true

为什么foo.__proto__.bar = bar;不好?如果它的坏不是Foo.prototype.bar = bar;一样坏?

那么为什么会出现这个警告:它非常慢并且不可避免地减慢了现代 JavaScript 实现中的后续执行速度当然Foo.prototype.bar = bar;也没有那么糟糕。

更新也许突变意味着重新分配。请参阅接受的答案。

4个回答
// This is bad: 
//foo.__proto__.bar = bar;

// But this is okay
Foo.prototype.bar = bar;

不。两者都在做同样的事情(如foo.__proto__ === Foo.prototype),并且都很好。他们只是barObject.getPrototypeOf(foo)对象创建一个属性

该语句所指的是分配给__proto__属性本身:

function Employee() {}
var fred = new Employee();

// Assign a new object to __proto__
fred.__proto__ = Object.prototype;
// Or equally:
Object.setPrototypeOf(fred, Object.prototype);

Object.prototype页面的警告更详细:

根据现代 JavaScript 引擎优化属性访问的性质,改变对象的 [[Prototype]] 是一个非常缓慢的操作

他们只是说改变一个已经存在的对象的原型链会扼杀优化相反,您应该通过Object.create().

我找不到明确的参考,但如果我们考虑V8 的隐藏类如何实现的,我们可以看到这里可能会发生什么。当改变一个对象的原型链时,它的内部类型会改变——它不像添加属性那样简单地变成一个子类,而是完全交换。这意味着所有属性查找优化都将被刷新,并且需要丢弃预编译的代码。或者它只是回退到未优化的代码。

一些值得注意的引述:

  • Brendan Eich(你认识他)说

    可写的 __proto__ 实现起来非常痛苦(必须序列化到循环检查),它会产生各种类型混淆的危险。

  • 布赖恩哈克特 (Mozilla) 说

    允许脚本对几乎任何对象的原型进行变异会使推断脚本的行为变得更加困难,并使 VM、JIT 和分析实现更加复杂和错误。由于可变的 __proto__ ,类型推断有几个错误,并且由于这个特性不能保持几个理想的不变量(即“类型集包含可以为 var/property 实现的所有可能的类型对象”和“JSFunctions 的类型也是函数” )。

  • 杰夫沃尔登说

    创建后的原型突变,其不稳定的性能不稳定,以及对代理和 [[SetInheritance]] 的影响

  • 埃里克·科里(谷歌)说

    我不期望使 proto 不可覆盖带来巨大的性能提升。在未优化的代码中,您必须检查原型链,以防原型对象(而不是它们的身份)已更改。在优化代码的情况下,如果有人写入 proto,您可以回退到未优化的代码。所以它不会有太大的不同,至少在 V8 曲轴中是这样。

  • 埃里克·浮士德 (Mozilla) 说

    当您设置 __proto__ 时,您不仅会破坏 Ion 未来对该对象进行优化的任何机会,而且还会迫使引擎爬行到所有其他类型推断(有关函数返回值的信息,或属性值,也许)认为他们知道这个对象并告诉他们也不要做太多假设,这涉及进一步的去优化和现有 jitcode 的失效。
    在执行过程中改变一个对象的原型真的是一个令人讨厌的大锤,我们唯一要避免出错的方法就是安全,但安全是缓慢的。

@BT 谢谢,已修复
2021-03-12 18:55:34
object.create 和proto之间存在显着的性能差异jsperf.com/proto-vs-object-create2感谢您的时间
2021-03-20 18:55:34
@OliverSieweke 虽然我没有任何真正的洞察力,但我预计不会有任何问题,并认为该const child = Object.setPrototypeOf({ method() { super.method() } }, parent)模式很好。引擎应该能够优化这一点,如果他们不这样做,我会提交功能请求。正如您所说,这是使方法在对象文字中工作的唯一方法,对于具有自定义原型链的数组或函数也是必需的。在这些情况下使用它。
2021-03-26 18:55:34
我想我们都阅读了 OP 链接到的页面。什么那些特定的优化?
2021-03-28 18:55:34
根据这一点,通过变异他们意味着重新分配. 在这种情况下,使用fred = Object.create(Object.prototype)应该同样糟糕。但他们特别说它Instead, create the object with the desired [[Prototype]] using Object.create.我认为你是对的。他们可能已经优化了Object.create.
2021-03-30 18:55:34

__proto__/setPrototypeOf与分配给对象原型不同。例如,当您有一个分配有成员的函数/对象时:

function Constructor(){
    if (!(this instanceof Constructor)){
        return new Constructor();
    } 
}

Constructor.data = 1;

Constructor.staticMember = function(){
    return this.data;
}

Constructor.prototype.instanceMember = function(){
    return this.constructor.data;
}

Constructor.prototype.constructor = Constructor;

// By doing the following, you are almost doing the same as assigning to 
// __proto__, but actually not the same :P
var newObj = Object.create(Constructor);// BUT newObj is now an object and not a 
// function like !!!Constructor!!! 
// (typeof newObj === 'object' !== typeof Constructor === 'function'), and you 
// lost the ability to instantiate it, "new newObj" returns not a constructor, 
// you have .prototype but can't use it. 
newObj = Object.create(Constructor.prototype); 
// now you have access to newObj.instanceMember 
// but staticMember is not available. newObj instanceof Constructor is true

// we can use a function like the original constructor to retain 
// functionality, like self invoking it newObj(), accessing static 
// members, etc, which isn't possible with Object.create
var newObj = function(){
    if (!(this instanceof newObj)){   
        return new newObj();
    }
}; 
newObj.__proto__ = Constructor;
newObj.prototype.__proto__ = Constructor.prototype;
newObj.data = 2;

(new newObj()).instanceMember(); //2
newObj().instanceMember(); // 2
newObj.staticMember(); // 2
newObj() instanceof Constructor; // is true
Constructor.staticMember(); // 1

每个人似乎都只关注原型,而忘记了函数可以将成员分配给它并在变异后实例化。如果不使用__proto__/ ,目前没有其他方法可以做到这一点setPrototypeOf几乎没有人使用无法从父构造函数继承的构造函数,并且Object.create无法提供服务。

另外,这是两个Object.create调用,目前在 V8(浏览器和 Node)中非常慢,这是__proto__一个更可行的选择

是的 .prototype= 也一样糟糕,因此措辞“不管它是如何实现的”。原型是一个伪对象,用于在类级别扩展功能。它的动态特性会减慢脚本的执行速度。另一方面,在实例级别添加函数产生的开销要少得多。

需要更多的上下文。从链接的资源我的理解它具体涉及到突变的的[prototype]对象。因此,对的分配Fn.prototype并不“同样糟糕”,因为它是在 create复制的(问题的重点是改变原型对象。)
2021-03-15 18:55:34
Adding a function on the instance level...incurs far less overhead. - 直到你有很多实例。
2021-03-22 18:55:34

这是使用节点的基准测试 v6.11.1

NormalClass : 普通类,原型未编辑

PrototypeEdited : 修改了原型的类(test()增加功能)

PrototypeReference : 添加了test()引用外部变量的原型函数的类

结果 :

NormalClass x 71,743,432 ops/sec ±2.28% (75 runs sampled)
PrototypeEdited x 73,433,637 ops/sec ±1.44% (75 runs sampled)
PrototypeReference x 71,337,583 ops/sec ±1.91% (74 runs sampled)

如您所见,原型编辑类比普通类快得多。具有引用外部变量的原型是最慢的,但这是一种用已经实例化的变量编辑原型的有趣方式

来源 :

const Benchmark = require('benchmark')
class NormalClass {
  constructor () {
    this.cat = 0
  }
  test () {
    this.cat = 1
  }
}
class PrototypeEdited {
  constructor () {
    this.cat = 0
  }
}
PrototypeEdited.prototype.test = function () {
  this.cat = 0
}

class PrototypeReference {
  constructor () {
    this.cat = 0
  }
}
var catRef = 5
PrototypeReference.prototype.test = function () {
  this.cat = catRef
}
function normalClass () {
  var tmp = new NormalClass()
  tmp.test()
}
function prototypeEdited () {
  var tmp = new PrototypeEdited()
  tmp.test()
}
function prototypeReference () {
  var tmp = new PrototypeReference()
  tmp.test()
}
var suite = new Benchmark.Suite()
suite.add('NormalClass', normalClass)
.add('PrototypeEdited', prototypeEdited)
.add('PrototypeReference', prototypeReference)
.on('cycle', function (event) {
  console.log(String(event.target))
})
.run()
这些示例都不涉及更改任何对象的 [[prototype]] 槽(通过写入.__proto__或调用Object.setProtottypeOf(),因此基准测试虽然有趣,但与提出的问题无关。
2021-03-29 18:55:34