Object lookup vs Object in (v3)

Revision 3 of this benchmark created on


Description

Simple benchmark to test speed of object lookup vs object in statement TL;DR Faster in all browsers, but by varying degrees

Setup

const SOURCE = [
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,0
]
function double(x) {
  return 2 * x
}

Test runner

Ready to run.

Testing in
TestOps/sec
object
let handlers = {
	1: double,
	2: double,
	3: double,
	4: double,
	5: double,
	6: double,
	7: double,
	8: double,
	9: double,
	0: double,
}
function handle(x) {
  handlers[x](x)
}
// function call, object lookup, function call
SOURCE.map(handle)
ready
in
let handlers = {
	1: double,
	2: double,
	3: double,
	4: double,
	5: double,
	6: double,
	7: double,
	8: double,
	9: double,
	0: double,
}
function handle(x) {
	if(x in handlers) {
		handlers[x](x)
	}
}
// function call, switch, function call
SOURCE.map(handle)
ready

Revisions

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