Use hasattr instead of dir

`dir()` is insanely slow since it has to create and sort a list each time. Switching to `hasattr` provides a nice 10x speedup.