javascript: optimize branches