Arrow function vs method vs declared function

Benchmark created on


Setup

"use strict";
class Test {
    constructor(N) {
        const array = this.array = Array(N);
        for (let i = 0; i < N; ++i) {
            array[i] = ~~(Math.random() * 100);
        }
    }
    doubleMethod(x) {
        return x * 2;
    }
    useCallback(callback) {
        const array = this.array;
        const len = array.length;
        let y;
        for (let i = 0; i < len; ++i)
            y = callback(array[i]);
    }
    noop() {
        const array = this.array;
        const len = array.length;
        let y;
        for (let i = 0; i < len; ++i)
            y = array[i];
    }
    inlined() {
        const array = this.array;
        const len = array.length;
        let y;
        for (let i = 0; i < len; ++i)
            y = array[i] * 2;
    }
}
function doubleFunction(x) {
    return x * 2;
}
const tester = new Test(10000);

Test runner

Ready to run.

Testing in
TestOps/sec
Arrow function
'use strict';
tester.useCallback(x => x * 2);
ready
Standalone function
'use strict';
tester.useCallback(doubleFunction);
ready
Method
'use strict';
tester.useCallback(tester.doubleMethod);
ready
Noop
'use strict';
tester.noop();
ready
Inlined
'use strict';
tester.inlined();
ready

Revisions

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