el_scheduler v1.0.61
Event loop scheduler
For use inside CPU bounded code, that doesn't contain async functions - to create non-blocking behavior.
Introduction
My reason for writing this module was that running event loop with setImmediate too often slows down everything much. Solution to this is to run e.l. after some time passed, and then reset time counter.
Scheduling interval can be any number, but not too large or less than 1 ms.
Note
This module is not intended for usage in your projects. I written it as experiment to learn how event loop works. Extensive tests have shown problem of latency.
Donate
My income consists of donations for my projects. If this module is useful to you, consider making a donation!
You can donate using PayPal or Wire transfer.
Usage
Instantiate a new E.L.S. with var els = new event_loop_sch(60);
. Constructor parameter represents scheduling time in ms.
Function inside which E.L.S. will be run, must be generator function (note the * sign). First we must check with ready(), if enough time has passed. Then we yield els.run()
.
Example
function* bubbleSort(items, el_scheduler)
{
var length = items.length;
for (var i = 0; i < length; i++)
{
// ready() returns true only after 60 ms has passed, run() is called, and time counter is being reset
if (el_scheduler.ready() == true) yield el_scheduler.run();
for (var j = 0; j < (length - i - 1); j++)
{
if (el_scheduler.ready() == true) yield el_scheduler.run();
if(items[j] > items[j+1])
{
var tmp = items[j];
items[j] = items[j+1];
items[j+1] = tmp;
}
}
}
}
async(function * ()
{
var els_instance = new event_loop_sch(60); // 60 ms interval
var floats_array = [];
for (var i = 0; i < 5000; i++)
{
floats_array.push( Math.random() * 10.0);
}
yield bubbleSort(floats_array, els_instance);
});