baconjs / bacon.js

Functional reactive programming library for TypeScript and JavaScript

Home Page:https://baconjs.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

High resolution time scheduler

semmel opened this issue · comments

Is that a useful perspective or option to increase the time resolution in Bacon.js?

What I'd like Bacon to do is:

// Rate limiting incoming data events on web sockets (prevents DOS attacks)
var incoming = Bacon.fromEvent(socket, 'message').map('.data'); // that works fine
var rateLimitedIncomingData = incoming.bufferWithTime(5)  // every 5 msecs
.map(R.take(20))    // maximum 20 events
.flatMap(events => Bacon.sequentially(5 / 20, events))  // that is 4 events per 1 msec evenly distributed

However I cannot do that because 5 / 20 = 0.25 is below the minimum time resolution of setTimeout in Node.js and the browser.

Is it useful at all to schedule function calls in JavaScript with a precision of many microseconds or is this diametrical to the event loop internals in Node.js or the browser?

While I could write quickly a Node.js C++ add-on for microsecond callbacks and hack Bacon.scheduler.setTimeout to use microsecond precision with my add-on, I wonder if anything would break with Bacon.js' scheduling?

Reason for wanting a C++ add-on:
All callback based high-resolution timeout libraries I've found are based on tight setImmediate loops querying high performance timers. That's to bad since they all suck up my CPU time!

Sounds interesting!

Bacon.js uses the Bacon.scheduler object for all scheduling so you can replace the scheduler object with a high-resolution one and it should work just fine.

If you try this, please tell how it worked and share your code!

I will post the progress on this issue when I got it working. I hope in a few weeks!