admin管理员组文章数量:1191752
In my most recent javascript program (which is mostly for fun and proof-of-concept than anything else) I have a lot of different kinds of objects and of each kind I'd have a fair amount of "instances". So I was thinking I should use classes for these, however since they are very simple I could simply construct them directly instead of using classes...
Example of what I mean:
//I'm making a "car" object that has the properties model, miles, value and color
//using a class like:
function car (model, miles, value, color) { .... }
//so I'd create a new car by using:
mycar = new car(model, miles, value, color);
//However for an object so simple I could also do:
mycar = {model: model, miles: miles, value: value, color: color};
I'm guessing the latter method would be more efficient in some way (no call to the class's function), but is it worth it?
To decide that I'd like to know about pros and cons of using classes vs using regular objects. Like do classes take up significantly more memory for example?
In my most recent javascript program (which is mostly for fun and proof-of-concept than anything else) I have a lot of different kinds of objects and of each kind I'd have a fair amount of "instances". So I was thinking I should use classes for these, however since they are very simple I could simply construct them directly instead of using classes...
Example of what I mean:
//I'm making a "car" object that has the properties model, miles, value and color
//using a class like:
function car (model, miles, value, color) { .... }
//so I'd create a new car by using:
mycar = new car(model, miles, value, color);
//However for an object so simple I could also do:
mycar = {model: model, miles: miles, value: value, color: color};
I'm guessing the latter method would be more efficient in some way (no call to the class's function), but is it worth it?
To decide that I'd like to know about pros and cons of using classes vs using regular objects. Like do classes take up significantly more memory for example?
Share Improve this question asked Oct 4, 2013 at 15:29 WingbladeWingblade 10.1k10 gold badges36 silver badges52 bronze badges 5- 3 use constructors when you need to inherit prototypes. use literals when you just need a collection of values for a task. – dandavis Commented Oct 4, 2013 at 15:32
- 1 It's down to OOP vs procedural programming. – webduvet Commented Oct 4, 2013 at 15:34
- 1 I would recommend always favoring readability and maintainability over performance. If it cleans up your code to have a class, go for it. You have the extra function in memory to define it, and the extra function call to invoke it. Unless the circumstance is extreme, the costs are negligible. – Scott Mermelstein Commented Oct 4, 2013 at 15:35
- Thanks for the very helpful answers so quickly :) I'll have to see if I could make good use of prototypes in this case and whether using classes will make it more readable, because it might just be the opposite for some small objects. – Wingblade Commented Oct 4, 2013 at 15:38
- possible duplicate of prototype based vs. class based inheritance – PW Kad Commented Oct 4, 2013 at 15:40
2 Answers
Reset to default 18In terms of performance, the difference comes when you add methods. If you use object literals each object needs to have a field for each method:
obj1--> { x: 10,
f1: method1,
f2: method2 }
obj2--> { x: 17,
f1: method1,
f2: method2 }
With classes, you can share common properties behind a shared prototype:
obj1--> { x:10,
__proto__: --------> { f1: method1,
} /----> f2: method2 }
|
obj2--> { x:17, |
__proto__: ---/
}
That said, the performance differences are only going to matter if you instantiate a lot of objects and those objects have many methods and many of those methods are closures. If I were you I would give a greater emphasis to code style issues: for example, with the object literal method you can use closures to simulate private variables while if the methods are in a shared public prototype then all your instance variables need to be public.
First of all, JavaScript is not class-based, what you have is just a special function. So what's the difference?
- An object instantiated by a constructor has an prototype object from which it inherits properties. This prototype object is shared by all object constructed by the same function. You can put common methods on there, which will be considerably faster.
- The constructor function is called. You can use it to initialise your object (fill in defaults, validate parameters, etc) and you have a scope to construct closures.
Even if for some objects you don't need prototypes, a function returning plain objects for you has still advantages. At least, it provides you the flexibility to change the implementation without the need to change every instantiation in your codebase. Therefore, start with a simple
function Car (model, miles, value, color) {
return {model: model, miles: miles, value: value, color: color};
}
(which you can even call with new
without effects) and extend it later if you need to.
本文标签: performanceJavascript classes vs objectspros and consStack Overflow
版权声明:本文标题:performance - Javascript classes vs objects, pros and cons? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738420777a2085849.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论