A mini scheduler for task handler in browser
Concurrency behavior make your code run faster
yarn add mini-scheduler
- time slicing (schedule long task not prevent render)
- concurrent (async pool support)
-
fast jobs
import { ts, TimeSlicingConfig } from 'mini-scheduler'; const arr = Array.from({ length: 20 }, (_, i) => i); const timeSlicingConfig: TimeSlicingConfig = { fps: 60, renderTime: 10, funcType: 'setTimeout', }; const fastTask = () => { const start = performance.now(); while (performance.now() < start + 5) {} console.log('long task done'); }; ts(arr, fastTask, timeSlicingConfig);
-
slow jobs
import { ts, TimeSlicingConfig } from 'mini-scheduler'; const arr = Array.from({ length: 20 }, (_, i) => i); const timeSlicingConfig: TimeSlicingConfig = { fps: 60, renderTime: 10, funcType: 'setTimeout', }; function* longTaskGenerator() { const start = performance.now(); while (performance.now() < start + 50) { yield; } console.log('long task done'); } ts(arr, longTaskGenerator, timeSlicingConfig);
-
slow job
import { tsGenerator, TimeSlicingConfig } from 'mini-scheduler'; const timeSlicingConfig: TimeSlicingConfig = { fps: 60, renderTime: 10, funcType: 'setTimeout', }; function* veryLongTaskGenerator() { const start = performance.now(); while (performance.now() < start + 500) { yield; } console.log('very long task done'); } tsGenerator(veryLongTaskGenerator(), timeSlicingConfig);
import { asyncPool } from 'mini-scheduler';
const arr = Array.from({ length: 20 }, (_, i) => i);
const task = <T>(item: T) => {
return new Promise((resolve) => {
window.setTimeout(async () => {
console.log(item, 'done');
resolve(item);
}, 2000);
});
};
asyncPool(2, arr, async (item, _) => {
await task(item);
});