This is a small library that let's you to split blocking loop operations over chunks, so that you can keep UI responsive.
Please try to execute the same code in different way and observe animation above.

Blocking Call

This is an example of non-chunked loop that blocks JavaScript, so that every animation code driven will be blocked

Please note, that executing this code will freeze your browser.

Simple Chunking

This case shows how to adopt a standard for-loop to the chunked-call implementation

The chunked-call expects to provide a task as the first parameter, and this function has to return either true to proceed, or false to stop

This will let the lib to decide whether continue calculation in this time frame, or wait for next frame. This method is significantly slower than the original code.

Grouped Chunking

Extended version that groups loop iteration

This implementation performs faster than the simple chunking as it doesn't waste time on callback on each iteration.

Downside is that it requires manual probing for groups size.

Adaptive Chunking

Dynamic version of Grouped Chunks that adjusts the size of group according to time spend on last operation.

It requires to add extra sanity checks (like minimum group size) in case that an external factor takes CPU power.

Main advantage is that it can adopt (to some extend) to external factors and limit operation in order to provide stable FPS.

// @task: A callback to function that executes until it returns false. 
// @callback: Called when execution is finished.
// @limitMs: Once execution time exceeded given time, it waits to a next frame
// @returns: An ID of chunked call execution
function setChunkedCall(task, callback, limitMs);

// @returns: A Promise thats wraps setChunkedCall
function setChunkedCallPromise(task, limitMs);

// Terminates execution of ongoing chunked call
// @id: Identifier of chunked call
// @returns: True if an execution has been killed successfully, false otherwise.
function killChunkedCall(id);