javascript: refactored tailcalls