Custom Idle Queue
This is a npm-module that lets you optimize the performance of important tasks by delaying background-tasks. It works a bit like requestIdleCallback but instead of fetching idle-time of the CPU, you can use this for any given limited ressource.
Quickstart
In this example we define database-requests
as limited ressource. We create an idleQueue arround all calls to the ressource to ensure our importantTask
is as fast as possible and the backgroundTask
only runs when no importantTask
is using the database.
npm install custom-idle-queue --save
// requireconst IdleQueue = ; // OR import; // create a new queueconst myQueue = ; // wrap all calls to your limited ressourceconst readFromDatabase = myQueue;const writeToDatabase = myQueue;const deleteFromDatabase = myQueue; // this is the important taskconst importantTask = { const oldNumber = await ; const newNumber = oldNumber++; await ; await ; return newNumber;}; // this is the background taskconst backgroundTask = { const newest = await ; const limitDate = - 1000*60*60; for let i = 0; i < newest; i++ const date = await ; ifdate < limitDate await ; } // we now run the backgroundTask in an intervall without slowing down the importantTaskasync whiletrue await myQueue; // wait until database-requests in idle await ; await ; // wait 2 seconds ; // if we now run the importantTask, it will not be slowed down by the backgroundTaskdocument ; // You can find the full documentation here https://github.com/pubkey/custom-idle-queue/blob/master/docs.md
Use cases
This module can be used on any limited ressource like
- HTTP-Requests
- Database-Calls
- Service-Worker-Calls
- Animations
Limitations
- IdleQueue cannot predict the future
When you start a backgroundTask
first and the importantTask
afterwards, the backgroundTask
will slow down the importantTask
because it is already running. To prevent this, you should use requestIdlePromise
as granular as possible. The backgroundTask-function from the example would be better when it awaits the idle-state before each usage of the limited ressource. This will ensure that the backgroundTask
will be paused until the importantTask
has finished.
// this is the background taskconst backgroundTask = { await myQueue; // request idle-state each time const newest = await ; const limitDate = - 1000*60*60; for let i = 0; i < newest; i++ await myQueue; // request idle-state each time const date = await ; ifdate < limitDate await myQueue; // request idle-state each time await ; }
- You cannot optimize CPU-only ressources
Because javascript runs in a single process, it doesn't make sense to define CPU as limited ressource. For example if you have a CPU-only-Function like calculatePrimeNumber
, you should not limit the access to the function with an idle-queue because at the time you call idleQueue.lock()
or idleQueue.wrapCall()
, the process will instantly run calculatePrimeNumber
before it even can change the idle-queue state.
Browser-Support
This module is using the Promise- and the Map-Object. If your runtime does not support them, you have to add them via polyfills.