Compare only_if implementations

Benchmark created on


Setup

function only_if_chain(condition) {
    return condition ? value => value : undefined;
}
function only_if_callback(condition, r) {
    if (!condition)
        return undefined;
    if (typeof r === 'function')
        return r();
    return r;
}

function only_if(condition, return_value) {
    return condition ? return_value : undefined;
}

let a = 0;
const c1 = false;
const c2 = true

Test runner

Ready to run.

Testing in
TestOps/sec
Current (master)
b = only_if(c1, a + 1);
b = only_if(c2, a + 1);
ready
Callback version
b = only_if_callback(c1, a + 1);
b = only_if_callback(c2, a + 1);
ready
Chain version
b = (_a = only_if_chain(c1)) === null || _a === void 0 ? void 0 : _a(1);
b = (_b = only_if_chain(c2)) === null || _b === void 0 ? void 0 : _b(1);
ready

Revisions

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