chrome 疑似克隆對象的優化?

图片描述
http://jsperf.com/immutable-vs-mutable

Safari 和 Firefox 均符合預期,二者性能相差無幾

var VectorMutable = function(x, y) { this.x = x; this.y = y; };
var Vector = function(x, y) { this.x = x; this.y = y; };

VectorMutable.prototype.clone = function() { return new VectorMutable(this.x, this.y); };
VectorMutable.prototype.add = function(x, y) { this.x += x; this.y += y; return this; };
Vector.prototype.add = function(x, y) { return new Vector(this.x+x, this.y+y); };

var result1 = new VectorMutable(0,0).clone().add(1,2);
var result2 = new Vector(0,0).add(1,2);

猜測是 chrome 對克隆對象的額外優化所致

阅读 1.9k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题