function main() {
const datas = new Array(10000).fill(null).map((_, i) => i)
function taskHanlder(_, i) {
console.log(i)
}
performChunkNode(datas, taskHanlder)
}
main()
function performChunk(datas, task, scheduler) {
if (typeof datas === 'number') {
datas = {
length: datas
}
}
if (datas.length === 0) {
return;
}
let i =0;
function _run() {
if (i >= datas.length) {
return;
}
scheduler((goOn) => {
while(goOn() > 0 && i < datas.length) {
task(datas[i], i)
i++
}
_run()
})
}
_run()
}
function performChunkBrowser(datas, taskHanlder) {
const scheduler = (task) => {
requestIdleCallback((idle) => {
task(() => idle.timeRemaining() > 0)
})
}
performChunk(datas, taskHanlder, scheduler)
}
function performChunkNode(datas, taskHanlder) {
const scheduler = (task) => {
setTimeout(() => {
const now = performance.now()
task(() => performance.now() - now <= 10)
}, 1000)
}
performChunk(datas, taskHanlder, scheduler)
}