Run details:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/117.0
Firefox 117
Ubuntu
Desktop
one year ago
Test name Executions per second
Function approach 4674214.0 Ops/sec
Object approach 4625951.0 Ops/sec
Script Preparation code:
x
 
function createFunctionBench() {
    let value = null;
    return function closure(val) {
        if (val) {
            value = val;
        }
        return value;
    };
}
class ClassBench {
    update(val) {
        if (val) {
            this.value = val;
        }
        return this.value;
    }
}
bench = {
    fn: createFunctionBench(),
    cls: new ClassBench()
};
Tests:
  • Function approach

     
    bench.fn(performance.now());
  • Object approach

     
    bench.cls.update(performance.now());