sanitize_sashe

Benchmark created on


Setup

const inputParams = { top: 10, right: 20, bottom: 30, left: 40 };

// Running the original constructor
function testOriginal() {
    new Original(inputParams);
}

// Running the optimized constructor
function testOptimized() {
    new Optimized(inputParams);
}

Test runner

Ready to run.

Testing in
TestOps/sec
Original
class Original {
    constructor(param) {
        if (param == null) {
            param = {};
        }
        let {top, right, bottom, left} = param;
        this.top = this._sanitizePad(top);
        this.right = this._sanitizePad(right);
        this.bottom = this._sanitizePad(bottom);
        this.left = this._sanitizePad(left);
    }

    _sanitizePad(value) {
        return value == null ? 0 : value;
    }
}
ready
Optimized
class Optimized {
    constructor({top, right, bottom, left} = {}) {
        const pads = [top, right, bottom, left].map(this._sanitizePad);
        [this.top, this.right, this.bottom, this.left] = pads;
    }

    _sanitizePad(value) {
        return value == null ? 0 : value;
    }
}
ready

Revisions

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