Replace a very expensive Object.keys() call with a simple counter