Skip to content

Instantly share code, notes, and snippets.

@kearfy
Last active August 13, 2023 14:40
Show Gist options
  • Select an option

  • Save kearfy/aa17fb1a1089e7f0bd64212aff10a049 to your computer and use it in GitHub Desktop.

Select an option

Save kearfy/aa17fb1a1089e7f0bd64212aff10a049 to your computer and use it in GitHub Desktop.
Simple TS batching function
// Sorry for the indentation size!
// Github does not let me change it and applied the wrong setting!
function batches<T>(unbatched: T[], batchSize: number) {
const numOfBatches = Math.ceil(unbatched.length / batchSize);
const batchIndexes = Array.from(
new Array(numOfBatches),
(_, i) => i * batchSize)
);
const batches = batchIndexes.map((i) =>
unbatched.slice(i, i + batchSize)
);
return {
unbatched,
batchSize,
numOfBatches,
batchIndexes,
batches,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment