This whole thing could be reduced to a three-line `for`/`in` loop. Don't get hung up on using `const` or functional programming for the sake of using either. JavaScript will still be slow.