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,
};
}
const add = (pointa, pointb) =>({ x: pointa.x + pointb.x, y: pointb.y + pointb.y });
const sub = (pointa, pointb) =>({ x: pointa.x - pointb.x, y: pointa.y - pointb.y });
var p1 = Point(10, 10);
var p2 = Point(10, -10);
var sum = add(p1, p2);
var dif = sub(p1, p2);
function Point(x, y){
return {
x,
y,
};
}
const add = ({ x:xa, y:ya }, { x:xb, y:yb }) =>({ x: xa + xb, y: ya + yb });
const sub = ({ x:xa, y:ya }, { x:xb, y:yb }) =>({ x: xa - xb, y: ya - yb });
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 | |
Basic function | |
Basic functions with object destructuring |
Test name | Executions per second |
---|---|
ES6 Class | 3524369.0 Ops/sec |
Function Prototype | 4096837.5 Ops/sec |
Object Literal | 2365982.8 Ops/sec |
Basic function | 65396164.0 Ops/sec |
Basic functions with object destructuring | 63556236.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested in this benchmark.
The test cases compare four different approaches to constructing class objects or functions: ES6 Classes, Function Prototypes, Object Literals with functions, and Basic Functions with object destructuring. The goal is to determine which approach is faster and more memory-efficient for these specific use cases.
ES6 Class
In the ES6 Class approach, we have a Point
constructor that takes two arguments, x
and y
, and returns an instance of the class. We also define two methods, add
and sub
, which create new instances of the Point
class with modified coordinates.
Pros:
Cons:
Point
class during method calls, leading to increased memory usage.Function Prototype
In this approach, we define a Point
function that takes two arguments, x
and y
, and returns an object with properties for x
and y
. We also define two methods, add
and sub
, as functions on the Point
prototype.
Pros:
Point
class.Cons:
Point
function and its methods, making it harder to modify or extend.Object Literal
In this approach, we define a Point
object literal that takes two arguments, x
and y
, and returns an object with properties for x
and y
. We also define two functions, add
and sub
, which are attached to the same instance of the Point
object.
Pros:
Cons:
add
and sub
functions modify the x
and y
properties of the same instance, leading to shared state.Basic Functions with Object Destructuring
In this approach, we define two separate functions, add
and sub
, which take a Point
object as an argument and return a new Point
object with modified coordinates. We use object destructuring to extract the x
and y
properties from the input object.
Pros:
Point
function and its methods, making it easier to modify or extend.Cons:
add
and sub
functions are complex or perform multiple operations on the input object.In summary, the benchmark results show that:
These results suggest that the Basic Functions with Object Destructuring approach is a good choice for this specific use case, as it provides a good balance between conciseness and performance. However, the optimal approach may depend on the specific requirements and constraints of your project.