class BaseClass {
constructor() {}
}
class Point extends BaseClass {
constructor(x, y) {
super();
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 Point1 = Point;
function BaseConstructor() {
}
function Point2(x, y) {
this.x = x;
this.y = y;
}
Point2.prototype.add = function(point) {
return new Point2(this.x + point.x, this.y + point.y);
}
Point2.prototype.sub = function(point) {
return new Point2(this.x - point.x, this.y - point.y);
}
Object.setPrototypeOf(Object.setPrototypeOf(Point2, BaseConstructor).prototype, BaseConstructor.prototype);
function Point3(x, y) {
return Object.setPrototypeOf({
x,
y,
add: (point) => Point3(this.x + point.x, this.y + point.y),
sub: (point) => Point3(this.x - point.x, this.y - point.y)
}, BaseConstructor.prototype);
}
var p1 = new Point1(10, 10);
var p2 = new Point1(10, -10);
var sum = p1.add(p2);
var dif = p1.sub(p2);
var p1 = new Point2(10, 10);
var p2 = new Point2(10, -10);
var sum = p1.add(p2);
var dif = p1.sub(p2);
var p1 = Point3(10, 10);
var p2 = Point3(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 |
Test name | Executions per second |
---|---|
ES6 Class | 32088148.0 Ops/sec |
Function Prototype | 51813656.0 Ops/sec |
Object Literal | 405987.8 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and considered.
Benchmark Definition:
The benchmark tests three different techniques for constructing class objects in JavaScript:
class
keyword to define a class.Script Preparation Code:
The script defines three classes:
BaseClass
: A base class with a constructor that does nothing.Point
(extends BaseClass
): A class that extends BaseClass
and has methods for adding and subtracting points.Point2
(using function prototype): A class that uses a function as its constructor, which returns an object with properties and methods.Point3
(using object literal): An object literal that sets up the prototype chain for Point2
.Individual Test Cases:
Each test case creates two instances of each class (p1
and p2
) with different values for x
and y
, and then calls the add()
and sub()
methods on these instances. The benchmark measures the execution time and memory usage for each test case.
Comparison:
The benchmark compares the performance of the three techniques:
Point2
)Point3
)Pros and Cons:
Point2
):Point3
):Library/Features Used:
class
keyword (ES6)Point2
)Point3
)super()
keyword (used in ES6 Class)Special JS Features/Syntax:
The benchmark uses some modern JavaScript features, such as:
extends
keyword (used in ES6 Class and Function Prototype)super()
keyword (used in ES6 Class)Point3
)Point3
, e.g., return Point3(this.x + point.x, this.y + point.y)
)Alternatives:
If you don't want to use classes or function prototypes, you can also consider using:
method()
syntax.Keep in mind that these alternatives may have different trade-offs in terms of performance, readability, and maintainability.