1.0.61 • Published 6 years ago

el_scheduler v1.0.61

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

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);
});
1.0.61

6 years ago

1.0.60

6 years ago

1.0.59

6 years ago

1.0.58

6 years ago

1.0.57

6 years ago

1.0.56

6 years ago

1.0.55

6 years ago

1.0.53

6 years ago

1.0.52

6 years ago

1.0.51

6 years ago

1.0.50

6 years ago

1.0.49

7 years ago

1.0.48

8 years ago

1.0.47

8 years ago

1.0.46

8 years ago