optimizing compare docs function