简体   繁体   English

v8性能准则

[英]v8 performance guidelines

I tend to use the factory method to construct my objects in javascript without the this keyword. 我倾向于使用工厂方法在没有this关键字的情况下在javascript中构造对象。 For example: 例如:

var PointA = function(x, y) {
  var z = {};
  z.x = x;
  z.y = y;
  return z;
};
var z0 = PointA(1, 2);

I've seen several examples illustrating how v8 uses hidden classes to attain fast property lookup, but all these examples employ the standard technique of object construction, namely: 我已经看到了几个示例,这些示例说明了v8如何使用隐藏类来实现快速属性查找,但是所有这些示例都采用了对象构造的标准技术,即:

var PointB = function(x, y) {
  this.x = x;
  this.y = y;
};
var z0 = new PointB(1, 2);

If I use a nontraditional factory like PointA am I thwarting the hidden class optimizers of v8? 如果我使用PointA这样的非传统工厂,是否可以阻止v8的隐藏类优化器?

All JavaScript objects in V8 are using hidden classes machinery, so it does not matter whether you create an object via a constructor or from a literal --- both will have hidden classes. V8中的所有JavaScript对象都使用隐藏类机制,因此无论是通过构造函数还是通过文字创建对象都没关系-两者都将具有隐藏类。

What matters here however is how hidden classes are connected to each other. 但是,这里重要的是隐藏类如何相互连接。 Hidden classes are forming transition trees with a root attached to the constructor. 隐藏的类正在形成过渡树,其根附加到构造函数。 If you are creating objects from a literal it means your transition tree is essentially growing from the Object constructor. 如果要从文字中创建对象,则意味着过渡树实际上是从Object构造函数中增长的。 This can confuse V8 sometimes because transition trees that are logically disconnected seem to be connected to V8 at their root. 这有时会使V8感到困惑,因为在逻辑上断开连接的过渡树似乎在其根部已连接到V8。 Take a look at this jsPerf microbenchmark. 看看这个 jsPerf微基准测试。 I create two objects that are seemingly disconnected: 我创建了两个似乎断开连接的对象:

function X() {
  var self = {};
  self.x = 0.1;
  return self;
}

function Y() {
  var self = {};
  self.x = true;
  return self;
}

var y = new Y, x = new X;

Yet internally V8 can't select the most efficient representation for the property x on objects made by X because it also sees objects made by Y as part of the same hidden class transition tree. 然而,内部V8不能选择该属性的最有效的代表性x上所作的对象X ,因为它也看到了由人造物体Y一样隐藏类别转换树的一部分。 If you make an object through a constructor that starts a new transition tree and a result there is no type polution. 如果通过启动新过渡树并生成结果的构造函数创建对象,则不会造成类型污染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM