Javascript原型操作员性能:节省内存,但速度更快吗? [英] Javascript prototype operator performance: saves memory, but is it faster?

查看:88
本文介绍了Javascript原型操作员性能:节省内存,但速度更快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用原型运算符在 here(Douglas Crockford)中阅读Javascript类的方法还保存内存

I read here (Douglas Crockford) using prototype operator to add methods to Javascript classes saves also memory.

然后我读了这篇John Resig的文章 用一堆原型属性实例化一个函数是非常,非常,快速,但他是在谈论以标准方式使用原型,还是他在文章中谈论他的具体例子?

Then I read in this John Resig's article "Instantiating a function with a bunch of prototype properties is very, very, fast", but is he talking about using prototype in the standard way, or is he talking about his specific example in his article?

例如,正在创建这个对象:

For example, is creating this object:

function Class1()
{
   this.showMsg = function(string) { alert(string); }
}
var c = new Class1();
c.showMsg();

慢于创建此对象,然后呢?

slower than creating this object, then?

function Class1() {}
Class1.prototype.showMsg = function(string) { alert(string); }
var c = new Class1();
c.showMsg();

P.S。

我知道原型用于创建继承和单例对象等。但是这个问题与这些主题没有任何关系。

I know prototype is used to create inheritance and singleton object etc. But this question does not have anyhting to do with these subjects.

编辑:对于 JS对象和JS静态对象之间的性能比较,可能感兴趣的人可以读取以下答案静态对象肯定更快,显然只有在您不需要多个对象实例时才可以使用它们。

to whom it might be interested also in performance comparison between a JS object and a JS static objet can read this answer below. Static object are definitely faster, obviously they can be usued only when you don't need more than one instance of the object.

推荐答案

这是一个有趣的问题,所以我运行了一些非常简单的测试(我应该重新启动我的浏览器以清除内存,但我没有;拿它来获取它的价值)。看起来至少在Safari和Firefox上, prototype 的运行速度要快得多。我确信使用功能齐全的对象进行真实测试将是一个更好的比较。我运行的代码就是这个(我分别运行了几次测试):

It was an interesting question, so I ran some very simple tests (I should have restarted my browsers to clear out the memory, but I didn't; take this for what it's worth). It looks like at least on Safari and Firefox, prototype runs significantly faster [edit: not 20x as stated earlier]. I'm sure a real-world test with fully-featured objects would be a better comparison. The code I ran was this (I ran the tests several times, separately):

var X,Y, x,y, i, intNow;

X = function() {};
X.prototype.message = function(s) { var mymessage = s + "";}
X.prototype.addition = function(i,j) { return (i *2 + j * 2) / 2; }

Y = function() {
    this.message = function(s) { var mymessage = s + "";}
    this.addition = function(i,j) { return (i *2 + j * 2) / 2; }
};


intNow = (new Date()).getTime();
for (i = 0; i < 1000000; i++) {
    y = new Y();
    y.message('hi');
    y.addition(i,2)
}
console.log((new Date()).getTime() - intNow); //FF=5206ms; Safari=1554

intNow = (new Date()).getTime();
for (i = 0; i < 1000000; i++) {
    x = new X();
    x.message('hi');
    x.addition(i,2)
}
console.log((new Date()).getTime() - intNow);//FF=3894ms;Safari=606

这真是一种耻辱,因为我真的讨厌使用原型。我喜欢我的目标代码是自封装的,不允许漂移。我想当速度很重要时,我没有选择。该死。

It's a real shame, because I really hate using prototype. I like my object code to be self-encapsulated, and not allowed to drift. I guess when speed matters, though, I don't have a choice. Darn.

非常感谢@Kevin指出我之前的代码错误,大大提升了原型的报告速度方法。修复后,原型仍然快得多,但差别不是很大。

Many thanks to @Kevin who pointed out my previous code was wrong, giving a huge boost to the reported speed of the prototype method. After fixing, prototype is still around significantly faster, but the difference is not as enormous.

这篇关于Javascript原型操作员性能:节省内存,但速度更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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