ES Class vs Prototype vs Use Define (v2)

Revision 2 of this benchmark created on


Setup

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
    add(b) {
        return new Point(this.x + b.x, this.y + b.y);
    }
}

var PointOld = /** @class */ (function () {
    function PointOld(x, y) {
        this.x = x;
        this.y = y;
    }
    PointOld.prototype.add = function (b) {
        return new PointOld(this.x + b.x, this.y + b.y);
    };
    return PointOld;
}());

class PointDefine {
    constructor(x, y) {
        Object.defineProperty(this, "x", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: x
        });
        Object.defineProperty(this, "y", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: y
        });
    }
    add(b) {
        return new PointDefine(this.x + b.x, this.y + b.y);
    }
}

Test runner

Ready to run.

Testing in
TestOps/sec
ES Class
new Point(1, 2).add(new Point(3, 4));
ready
Prototype
new PointOld(1, 2).add(new PointOld(3, 4));
ready
Use Define
new PointDefine(1, 2).add(new PointDefine(3, 4));
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.