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 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);
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);
class Base {
constructor(x, y){
this.x = x;
this.y = y;
}
}
class Point extends Base{
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);
class Base {
constructor(x, y){
this.x = x;
this.y = y;
}
}
class Add extends Base {
add(point){
return new Point(this.x + point.x, this.y + point.y);
}
}
class Point extends Add{
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);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
ES6 Class | |
Function Prototype | |
Object Literal | |
Class extends | |
class extends twce |
Test name | Executions per second |
---|---|
ES6 Class | 275767.2 Ops/sec |
Function Prototype | 216945.4 Ops/sec |
Object Literal | 794893.5 Ops/sec |
Class extends | 145156.5 Ops/sec |
class extends twce | 111965.1 Ops/sec |
The benchmark titled "ES6 Class vs Prototype vs Object Literal vs ES6 Class extends vs ES6 Class extends twice" aims to evaluate the performance and memory usage of five different techniques for constructing class-like objects in JavaScript. The following approaches are compared:
ES6 Class:
Point
using the ES6 class syntax, including a constructor and methods for addition and subtraction of points.Function Prototype:
Point
) and attaches methods to its prototype.Object Literal:
Point
creates a new object, which can lead to redundancy for shared methods.Class Extends (Single Inheritance):
Point
extends a Base
class.Class Extends (Multiple Inheritance with a Base Class):
The results show variation in "Executions Per Second" for each approach:
Alternatives: Other object construction techniques in JavaScript include using factory functions, which combine both instance and prototype patterns to create flexible objects, or leveraging ES6 modules for better organization of classes and data structures.
Performance Considerations: When selecting an approach, consider the performance implications based on how many objects need to be created and the expected complexity of the application.
Readability and Maintainability: Choose an approach that matches the team's proficiency and the development needs. ES6 class syntax may be preferred for new applications, while legacy code may still rely on prototypes.
Future Proofing: New syntax (like ES6 classes) might be consistently optimized in modern JavaScript engines, while older prototypes may become less favored.
This benchmark provides insights into not only performance but also use cases for each method, helping engineers choose the appropriate structure for their specific needs.