Run details:
Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0
Firefox 128
Linux
Desktop
29 days ago
Test name Executions per second
ES6 Class 169188.7 Ops/sec
Function Prototype 247208.2 Ops/sec
Object Literal 590021.8 Ops/sec
Tests:
  • ES6 Class

    x
     
    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;
    var p1 = new Point1(10, 10);
    var p2 = new Point1(10, -10);
    var sum = p1.add(p2);
    var dif = p1.sub(p2);
  • Function Prototype

     
    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);
    var p1 = new Point2(10, 10);
    var p2 = new Point2(10, -10);
    var sum = p1.add(p2);
    var dif = p1.sub(p2);
  • Object Literal

     
    function PointX(x, y) {
        return {}
    }
    function Point3(x, y) {
        return {
            ...PointX(x, y),
            x,
            y,
            add: (point) => Point3(x + point.x, y + point.y),
            sub: (point) => Point3(x - point.x, y - point.y)
        }
    }
    var p1 = new Point3(10, 10);
    var p2 = new Point3(10, -10);
    var sum = p1.add(p2);
    var dif = p1.sub(p2);