Define vs Binding

Benchmark created on


Setup

class Test1 {
    constructor() {
        this.foo = this.foo.bind(this);
    }
    foo() {
        return 5;
    }
}

class Test2 {
    constructor() {
        Object.defineProperty(this, "foo", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: () => {
                return 5;
            }
        });
    }
}

class Test3 {
    constructor() {}
    
    foo = () => 5;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Binding
new Test1()
ready
Defining
new Test2()
ready
Use native JS class field
new Test3()
ready

Revisions

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