Tangent: you and mourner are the only two JS developers I know of who inline recursive function calls into stacks+loops into typed arrays+loops as a performance optimization. Well, and me sometimes these days, but I just got inspired by mourner. I feel like it's a pretty obvious but weirdly overlooked technique, and not even *that* low-level - maybe nobody realizes that function calls can have significant overhead in tight loops in JS?
Yes, the main optimization I was aiming for was avoiding pushing many thousands of three-element arrays onto a stack, in favor of a fixed-storage typed array stack with a pointer. The two main reasons are 1) performance, and 2) garbage collection. I did keep a benchmark outputting as I optimized the code, and *sadly*, along with inlining some function calls, these heavy optimizations really did have a pretty significant impact.
Also, you should take a look at Mikola Lysenko's old code. :)
If you're interested, I've reworked the main scanline function to handle arbitrary directions, which allows additional effects like hard shadows which, using @mourner's Suncalc library could be physically accurate as well. TBH, it was a moderately challenging extension since every pixel must be touched *exactly* once. The final changes I'd like to make are:
1. start the horizon computation on parent tiles so that the boundaries are handled correctly.
2. check for consistency across zoom levels.
See: https://observablehq.com/d/506aa0e5a011e3fd If this works out, you could pretty much add this as a realtime effect.