rwaldron / temporal

Non-blocking, temporal task sequencing. For use with robots built with Johnny-Five
http://johnny-five.io/
MIT License
84 stars 12 forks source link

High res time #20

Closed dtex closed 6 years ago

dtex commented 6 years ago

Allow temporal to be passed sub-millisecond delays.

temporal.resolution(0.1);
temporal.delay(0.7, task => {
  //Do something
});

I used this performance test to make sure things actually worked better:

https://gist.github.com/dtex/8d24cbd5608c1bcb6b62456bf965ee61

dtex commented 6 years ago

Some observations about all this:

Running a resolution of 0.01 results in more frequent visits from the garbage collector, so it's really not recommended.

A resolution of 0.1 regularly outperforms 1.0 on a fast MBP, but will likely run slower on SBC's.

There doesn't appear to be any benefit to staying with Date.now() for >=1ms resolution, but again slower computers (PI's, T2's) might behave differently so I'm not changing the functionality until I've tested on more of those.

The 500 microsecond hits to Map.get() and Map.set() appear to be "smoothed out" by Date.now(). I think it's just a result of the way Date.now() works.