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);
function Point(x, y) {
return {
x,
y,
}
}
function add(a, b) {
return Point(a.x + b.x, a.y + b.y);
}
function sub(a, b) {
return Point(a.x - b.x, a.y - b.y);
}
var p1 = Point(10, 10);
var p2 = Point(10, -10);
var sum = add(p1, p2);
var dif = sub(p1, p2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
ES6 Class | |
Function Prototype | |
Object Literal | |
Object & Functions |
Test name | Executions per second |
---|---|
ES6 Class | 891463.4 Ops/sec |
Function Prototype | 833396.6 Ops/sec |
Object Literal | 1348489.8 Ops/sec |
Object & Functions | 245927984.0 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Overview
The benchmark compares the performance of four different techniques for constructing values and calling functions on them:
Options Compared
Each test case uses one of the above techniques, and their performance is compared across different browsers (Firefox 121) and devices (Desktop).
Pros and Cons of Each Approach
Library/Function Used
None of the test cases use a specific JavaScript library or framework.
Special JS Feature/Syntax
No special JavaScript features or syntax are used in the benchmark. The focus is on comparing different coding styles and their performance impact.
Other Alternatives
If you're interested in exploring alternative approaches, consider:
Keep in mind that these alternatives may have different performance characteristics, trade-offs, and use cases compared to the techniques tested in this benchmark.
In conclusion, this benchmark provides a straightforward comparison of four coding styles, offering insights into the performance implications of each approach. By understanding the pros and cons of each technique, developers can make informed decisions about which style to use depending on their specific needs and requirements.