JS has a recursion limit of 5k and also function calls in JS are way more costiler than the native ones, hence this is not a premature optimization, you can perf it with JS perf.