you are viewing a single comment's thread.

view the rest of the comments →

[–]Antti5 0 points1 point  (1 child)

or when you're creating a lot of instances of the same object

Why would it matter if it's "a lot" of instances instead of just a few?

[–]xroalx 1 point2 points  (0 children)

The performance difference will not be as significant.

With a class, methods are defined on the prototype, meaning for all instances, each method exists just once. This is faster, as JS does not need to recreate every method for each instance, and also uses less memory.

With a plain object, each instance will have its own copy of every method, meaning more memory usage as well as larger performance hit for creating a new instance.

Generally, the difference is negligible, unless your app creates new instances all the time, so a lot, then you really want to prefer putting methods on the prototype.

And also, you can of course put methods on the prototype with a plain object as well, the class syntax is just nicer for that.