与类和对象相关的 JavaScript 开销
我想知道与创建新类而不是该类的新对象相关的开销是小还是大。我正在使用 dojo,但我将提供纯 JS 的示例。我将在启动时创建 10 到 100 个对象,我认为这不会是一个严重的问题,但我想涵盖所有基础。
案例 1:Javascript 对象
function Person(name){
this.name = name;
}
var p1 = new Person('Caine');
var p2 = new Person('Seth');
var p3 = new Person('Abel');
与案例 2:Javascript 类
function Person1(){
this.name = 'Caine';
}
function Person2(){
this.name = 'Seth';
}
function Person3(){
this.name = 'Abel';
}
var p1 = new Person1();
var p2 = new Person2();
var p3 = new Person3();
编辑:人们想知道为什么我会采取这种方法。我正在实现一个模块化程序,用户可以根据需要创建和/或加载对象,而不是拥有一个 Person/Shape/Text... 类并使用 50,000,000 个参数(姓名、年龄、性别、标签、字体、 x , y , w , h...) 我想创建一个包含所有值的类。这也将简化代码的编辑,因为我希望允许用户从浏览器中查看和修改代码。我对 OOP 并不陌生,我确实意识到这偏离了标准编程过程,所以请相信我知道我在做什么 =)
I was wondering if the overhead associated with creating a new class instead of a new object of that class was small or large. I am using dojo, but I will provide versus examples for pure JS. I will be creating between 10 and 100 objects once at start up, I don't think this will be a serious issue, but I want to cover all my bases.
Case 1: Javascript object
function Person(name){
this.name = name;
}
var p1 = new Person('Caine');
var p2 = new Person('Seth');
var p3 = new Person('Abel');
Versus Case 2: Javascript Classes
function Person1(){
this.name = 'Caine';
}
function Person2(){
this.name = 'Seth';
}
function Person3(){
this.name = 'Abel';
}
var p1 = new Person1();
var p2 = new Person2();
var p3 = new Person3();
EDIT : people want to know why I would ever take this approach. I'm implementing a modular program where the user creates and/or loads objects as desired, and rather than having one Person/Shape/Text... class and calling it with 50,000,000 arguments (name, age, sex, label, font, x , y , w , h...) I want to create a single class with all the values. This will also simplify editing the code as I want to allow the user to view and modify the code from within the browser. I am not new to OOP, and I do realize this is a departure from standard programing procedure, so have a little faith in that i know what i am doing =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
并不是真正的类(JS 没有它们),但在第二个示例中你得到的是两个额外的构造函数。每个都必须有一个自己的附加
prototype
属性对象。因此,就创建和存储对象而言,第二种方法的效率稍低。但是,如果您有大量
Person1
实例,则第二种方法可以通过将共享属性放在原型上而不是单独放在每个新实例上来节省(少量)空间:保存一个 String 实例每个 Person1 实例。
实际上,它不会产生任何实际差异,您应该编写代码封装的思想的最清晰表达。 (我个人认为每个名字都有不同的类别是不寻常的......)
Not really classes as such (JS doesn't have 'em), but what you've got in the second example is two additional constructor functions. Each will necessarily have an additional
prototype
property object of its own.So your second approach is marginally less efficient in terms of objects created and stored. If you had lots of instances of
Person1
, though, the second approach could save (a tiny amount of) space by putting the shared property on the prototype instead of separately on each new instance:saving one String instance per Person1 instance.
In reality it's not going to make any practical difference and you should write whichever is the cleanest expression of the idea your code encapsulates. (Personally I think it'd be unusual to have a different class for each first name...)
我写了一个jsperf测试用例, http://jsperf.com/class-vs-object-perf ,演示 4 种方法,2 种使用您编写的案例,1 种使用返回对象并可以适应模块模式的函数,最后一种使用原型。
在 Chrome 14 上,1、2、4 之间的性能差异几乎相同。唯一慢的是类似于模块模式的模式。
I wrote up a jsperf test case, http://jsperf.com/class-vs-object-perf, to demonstrate 4 approaches, 2 using the cases you wrote, 1 using a function that returns an object and could be adapted into a module pattern, and the last using the prototype.
On Chrome 14 the performance difference between 1, 2, 4 are nearly identical. The only one that is slow is the one similar to the module pattern.