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 ?
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 outperformssetTimeout
in my text environment (Node.js 13.0.1).The line I'm referring to is
index.ts:79
, instead ofsetTimeout(() => 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 likesetTimeout
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 likesetTimeout
.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 withprocess.nextTick
. All of those are also faster thansetTimeout
, 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 ofsetTimeout
?Related:
This StackOverflow thread on NodeJS' setTimeout(fn,0) vs setImmediate(fn)
Node's Timers, setImmediate
Thank you for reading and for the good job!