sinclairzx81 / zero

A 3D renderer written in JavaScript and rendered to the terminal.
Other
2.41k stars 143 forks source link

Consider using setImmediate instead of setTimeout in the render loop? #9

Closed eugeniopacceli closed 4 years ago

eugeniopacceli commented 4 years ago

Hello!

This is a very cool project and also an impressive job!

I've been tinkering with the source code a little and I've noticed that in the render loop using Node's function setImmediate to set up the next iteration really outperforms setTimeout in my text environment (Node.js 13.0.1).

The line I'm referring to is index.ts:79, instead of

setTimeout(() => loop())

I've

setImmediate(() => loop())

or

setImmediate(loop)

with a substantial performance increase (on 120x28 the demo went from avg. ~65fps to ~250fps, on 237x70 it went from avg. ~30 fps to ~64 fps).

Functions set to run by setImmediate respect I/O events and their callbacks like setTimeout ones do. In this implementation it allows for the event handler that changes the rendering scene resolution to run on terminal window size change just like setTimeout.

Other tests I did were with a while(1) inside the loop function, converting the loop function to an async function and having it's callback call it again, and having the next iteration of the function set up to run with process.nextTick. All of those are also faster than setTimeout, but they seem to hang the process on the loop and disallow other event handlers and callbacks to run.

Is there a reason for why setImmediate isn't used in the place of setTimeout ?

Related:

Thank you for reading and for the good job!

sinclairzx81 commented 4 years ago

Hi, thanks :D

Will close this one as the setImmediate() call went in with optimizations enabling 24-bit true-color support. See PR https://github.com/sinclairzx81/zero/pull/10 for details.