性能:内联与原型定义的方法? [英] Performance: inline- vs. prototype-defined methods ?

查看:55
本文介绍了性能:内联与原型定义的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




遇到了性能问题;情况如下所述(参见Version

Inline),我有一个javascript类项目,定义了很多方法

里面和每次调用 ; new item()" js翻译人员必须查看所有方法定义(使用Venkman做个人资料)

,这对我来说需要太多时间..

如果我通过.prototype在

构造函数之外定义这些方法会有所不同(参见Version Prototype)??


版本内联:


item = function(){

this.myMethod1 = function(){..}

this .myMethod2 = function(){..}

this.myMethod3 = function(){..}

...

这个。 myMethod999 = function(){..}

}

版本原型:


item = function(){


}


item.prototype.myMethod1 = function(){..}

item.prototype.myMethod2 = function (){..}

item.prototype.myMethod3 = function(){..}

...

item.prototype。 myMethod999 = function(){..}


非常感谢提前!!


-

Gerald Stampfel
gs ******* @ sedisys.com

推荐答案

Gerald S写道:
Gerald S wrote:


得到了性能问题;情况如下所述(参见版本
内联),我有一个javascript类项目,内部定义了许多方法,每次调用new item() js翻译必须查看所有方法定义(使用Venkman做了一个配置文件)
这对我来说需要花费太多时间..

如果我有所作为通过.prototype在
构造函数之外定义这些方法(参见Version Prototype)??


got a performance problem; situation is as described below (see "Version
Inline"), i''ve got a javascript class item with many methods defined
inside and with every call to "new item()" the js interpreter has to
look through all the method definitions (did a profile using Venkman)
which takes too much time for me ..

would it make a difference if i define these methods outside the
constructor via .prototype (see "Version Prototype") ??



是的,它会有所不同,因为你只会设置方法

一次而不是每次你创建一个对象,如果你有很多

对象,你将使用更少的内存,所以可能有一个增益。调用

的方法可能会稍微慢一点,但这不太可能是明显的。


Yes it will make a difference since you will only be setting up the methods
once instead of every time you create an object, also if you have a lot of
objects you will be using less memory so there may be a gain there. Calls
to the methods may be slightly slower but that is unlikely to be
noticeable.


Duncan Booth写道:
Duncan Booth wrote:
是的它会有所不同,因为你只会设置一次方法而不是每次创建一个对象,如果你有很多
对象你将使用更少的内存,因此可能会有所收获。对方法的调用可能会稍慢但不太可能明显。
Yes it will make a difference since you will only be setting up the methods
once instead of every time you create an object, also if you have a lot of
objects you will be using less memory so there may be a gain there. Calls
to the methods may be slightly slower but that is unlikely to be
noticeable.




ok,谢谢!!


但如果我这样做.prototype-way,还有另外一个问题。考虑

以下情况:

item = function(){

var privateVar;


function doInternalStuff();

}


item.prototype.myMethod1 = function(){..}

item.prototype .myMethod2 = function(){..}

item.prototype.myMethod3 = function(){..}

...

item.prototype.myMethod999 = function(){..}

两个问题:

1)我无法从myMethodXXX方法访问privateVar(或者我可以吗? )

2)我不能用我的myMethodXXX方法调用doInternalStuff()


i可能会暴露给公众,但有没有办法保持他们

私人并使用它们。任何模式?


再次感谢..


-

Gerald Stampfel
gs ******* @ sedisys.com


Gerald S写道:
Gerald S wrote:
但如果我这样做.prototype-way,还有另一个问题。考虑以下情况:

item = function(){
var privateVar;

函数doInternalStuff();
}

item.prototype.myMethod1 = function(){..}
item.prototype.myMethod2 = function(){..}
item.prototype.myMethod3 = function(){ ..}
...
item.prototype.myMethod999 = function(){..}

两个问题:
1)我无法访问privateVar来自myMethodXXX方法(或者我可以吗?)2)我不能用myMethodXXX方法调用doInternalStuff()

我可以向公众公开,但是有没有让他们保持私密并使用它们的方法。任何模式?
but if i do it the .prototype-way, there is another problem. consider
the following situation:
item = function () {
var privateVar;

function doInternalStuff();
}

item.prototype.myMethod1 = function() { .. }
item.prototype.myMethod2 = function() { .. }
item.prototype.myMethod3 = function() { .. }
...
item.prototype.myMethod999 = function() { .. }
two issues:
1) i can''t access privateVar from my myMethodXXX methods (or can i?)
2) i can''t call doInternalStuff() from my myMethodXXX methods

i could expose both to the public, but is there a way to keep them
private AND use them. any patterns ?



Javascript并不真正支持私有变量。你最好的行动是

只是使用一些约定来表明它们是私有的(例如,

在名称上引出下划线),然后不从外部访问它们/>
适当的功能。


Javascript doesn''t really support private variables. Your best action is
simply to use some convention to indicate that they are private (e.g. a
leading underscore on the name) and then not access them from outside the
appropriate functions.


这篇关于性能:内联与原型定义的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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