Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ES6 Class vs Prototype vs Object Literal
(version: 0)
Test the speed and memory usage using 3 different techniques for constructing class objects.
Comparing performance of:
ES6 Class vs Function Prototype vs Object Literal
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
ES6 Class
class Point { constructor(x, y){ this.x = x; this.y = y; } add(point){ return new Point(this.x + point.x, this.y + point.y); } sub(point){ return new Point(this.x - point.x, this.y - point.y); } } var p1 = new Point(10, 10); var p2 = new Point(10, -10); var sum = p1.add(p2); var dif = p1.sub(p2);
Function Prototype
function Point(x, y){ this.x = x; this.y = y; } Point.prototype.add = function(point){ return new Point(this.x + point.x, this.y + point.y); } Point.prototype.sub = function(point){ return new Point(this.x - point.x, this.y - point.y); } var p1 = new Point(10, 10); var p2 = new Point(10, -10); var sum = p1.add(p2); var dif = p1.sub(p2);
Object Literal
function Point(x, y){ return { x, y, add: (point)=>Point(this.x + point.x, this.y + point.y), sub: (point)=>Point(this.x - point.x, this.y - point.y) } } var p1 = Point(10, 10); var p2 = Point(10, -10); var sum = p1.add(p2); var dif = p1.sub(p2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
ES6 Class
Function Prototype
Object Literal
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 146 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
ES6 Class
296127.8 Ops/sec
Function Prototype
260812.5 Ops/sec
Object Literal
1995189.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares three different approaches for constructing class objects in JavaScript: 1. ES6 Class (Constructor Function) 2. Function Prototype 3. Object Literal **Options Compared** * The benchmark measures the speed and memory usage of each approach. * Each option is compared on a specific test case, which involves creating two `Point` objects with different coordinates, performing addition and subtraction operations using these objects. **Pros and Cons of Each Approach** 1. **ES6 Class (Constructor Function)**: * Pros: Encapsulates data and behavior in a single unit, easier to read and maintain. * Cons: May have overhead due to the syntax and the need for explicit `this` binding. 2. **Function Prototype**: * Pros: Lightweight, fast, and flexible. Can be used as a mixin or inheritance mechanism. * Cons: Requires manual management of prototype chaining and method implementation. 3. **Object Literal**: * Pros: Minimalist, concise, and often preferred for simple objects. * Cons: May lack encapsulation and behavior organization, requires explicit property access. **Library Used** There is no library explicitly mentioned in the benchmark. However, the `Point` class definition uses a common pattern for prototypal inheritance, where methods are defined on the prototype object rather than an instance. **Special JS Feature/ Syntax** The benchmark uses ES6 features such as classes (ES6 Class), function prototypes (Function Prototype), and template literals (Object Literal). These features provide concise syntax for creating objects, but may not be compatible with older browsers or environments that don't support them. **Other Alternatives** If you need to compare other approaches for constructing class objects in JavaScript, consider the following alternatives: * **Prototype Chain**: Instead of using a constructor function, you can create a prototype chain by setting up an object as a prototype and then referencing it from another object. * **Class Factory Functions**: Use a factory function to create instances of a class. This approach provides a balance between encapsulation and flexibility. * **Object-Oriented Frameworks**: Utilize JavaScript frameworks like React, Angular, or Vue.js, which provide built-in support for classes and object-oriented programming. Keep in mind that each approach has its trade-offs, and the best choice depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
ES6 Class vs Prototype vs Object Literal v2
ES6 Class vs Prototype vs Object Literal 4
Comparison of classes vs prototypes vs object literals also including the inheritance
ES6 Class vs Prototype vs Object Literal v2 with Extends
ES6 Class vs Prototype vs Object Literal v3
Comments
Confirm delete:
Do you really want to delete benchmark?