Global function vs method calls

Benchmark created on


Setup

window.foo = x => x * x;
window.foo_module = { foo: x => x * x };
function Foo() {}
Foo.prototype.foo = x => x * x;

Test runner

Ready to run.

Testing in
TestOps/sec
foo()
let k = 0;
for (let i = 0; i < 10000; ++i) {
	k += foo(i);
}
window.fooOut = k;
ready
window.foo()
let k = 0;
for (let i = 0; i < 10000; ++i) {
	k += window.foo(i);
}
window.fooOut = k;
ready
module.foo()
const obj = foo_module;
let k = 0;
for (let i = 0; i < 10000; ++i) {
	k += obj.foo(i);
}
window.fooOut = k;
ready
object.foo()
const obj = new Foo();
let k = 0;
for (let i = 0; i < 10000; ++i) {
	k += obj.foo(i);
}
window.fooOut = k;
ready

Revisions

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