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

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

问题描述

在JavaScript中,我们有两种方法可以创建类并赋予它公共功能。

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

方法1:

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

方法2:

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给对象的每个实例赋予了它自己的函数副本(因此使用了更多的内存,更不用说分配所需的时间) - 这是实际发生的事情吗?看起来优化网络浏览器可以轻松实现e是识别这种非常常见的模式,并且实际上具有对象引用的所有实例通过这些构造函数定义的相同函数副本。然后,如果稍后明确更改,它只能为实例提供自己的函数副本。

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...

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

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