Skip to content

Instantly share code, notes, and snippets.

@khamiltonuk
Last active October 30, 2019 15:19
Show Gist options
  • Save khamiltonuk/410e366407d4d73a2f50 to your computer and use it in GitHub Desktop.
Save khamiltonuk/410e366407d4d73a2f50 to your computer and use it in GitHub Desktop.
Optimising JavaScript performances with asynchronous loops
function splitLoop(items, process, context, callback) {
var todo = items.concat();
setTimeout(function () {
var start = +new Date();
do {
process.call(context, todo.shift());
} while (todo.length > 0 && (+new Date() - start < 50));
if (todo.length > 0) {
setTimeout(arguments.callee, 25);
} else {
callback(items);
}
}, 25);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment