通过原型定义方法与在构造函数中使用 this - 真的有性能差异吗? [英] Defining methods via prototype vs using this in the constructor - really a performance difference?

查看:33
本文介绍了通过原型定义方法与在构造函数中使用 this - 真的有性能差异吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 JavaScript 中,我们有两种方法可以创建类"并为其提供公共函数.

In JavaScript, we have two ways of making a "class" and giving it public functions.

方法一:

function MyClass() {
    var privateInstanceVariable = 'foo';
    this.myFunc = function() { alert(privateInstanceVariable ); }
}

方法二:

function MyClass() { }

MyClass.prototype.myFunc = function() { 
    alert("I can't use private instance variables. :("); 
}

我已经多次阅读人们 使用方法 2 更有效,因为所有实例共享相同的函数副本,而不是每个实例都拥有自己的副本.但是,通过原型定义函数有一个巨大的缺点——它不可能拥有私有实例变量.

I've read numerous times people saying that using Method 2 is more efficient as all instances share the same copy of the function rather than each getting their own. Defining functions via the prototype has a huge disadvantage though - it makes it impossible to have private instance variables.

尽管,理论上,使用方法 1 为对象的每个实例提供了自己的函数副本(因此使用了更多的内存,更不用说分配所需的时间了) - 实际情况是这样吗?似乎 Web 浏览器可以轻松进行的优化是识别这种极其常见的模式,并且实际上让对象的所有实例引用通过这些构造函数"定义的函数的相同副本.那么它只能给一个实例自己的函数副本,如果它稍后被显式更改.

Even though, in theory, using Method 1 gives each instance of an object its own copy of the function (and thus uses way more memory, not to mention the time required for allocations) - is that what actually happens in practice? It seems like an optimization web browsers could easily make is to recognize this extremely common pattern, and actually have all instances of the object reference the same copy of functions defined via these "constructor functions". Then it could only give an instance its own copy of the function if it is explicitly changed later on.

任何关于两者性能差异的见解 - 或者更好的现实世界体验 - 都会非常有帮助.

Any insight - or, even better, real world experience - about performance differences between the two, would be extremely helpful.

推荐答案

参见 http://jsperf.com/prototype-vs-this

通过原型声明你的方法更快,但这是否相关是有争议的.

Declaring your methods via the prototype is faster, but whether or not this is relevant is debatable.

如果您的应用存在性能瓶颈,则不太可能出现这种情况,除非您碰巧在某个任意动画的每一步都实例化了 10000 多个对象,例如.

If you have a performance bottleneck in your app it is unlikely to be this, unless you happen to be instantiating 10000+ objects on every step of some arbitrary animation, for example.

如果性能是一个严重的问题,并且您想进行微优化,那么我建议通过原型声明.否则,请使用对您最有意义的模式.

If performance is a serious concern, and you'd like to micro-optimise, then I would suggest declaring via prototype. Otherwise, just use the pattern that makes most sense to you.

我要补充一点,在 JavaScript 中,有一个约定,即在属性前加上下划线(例如 _process()),这些属性旨在被视为私有的.大多数开发人员会理解并避免这些属性,除非他们愿意放弃社会契约,但在这种情况下,您还不如不去迎合他们.我的意思是说:你可能真的不需要 true 私有变量......

I'll add that, in JavaScript, there is a convention of prefixing properties that are intended to be seen as private with an underscore (e.g. _process()). Most developers will understand and avoid these properties, unless they're willing to forgo the social contract, but in that case you might as well not cater to them. What I mean to say is that: you probably don't really need true private variables...

这篇关于通过原型定义方法与在构造函数中使用 this - 真的有性能差异吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆