Last active
August 13, 2023 14:40
-
-
Save kearfy/aa17fb1a1089e7f0bd64212aff10a049 to your computer and use it in GitHub Desktop.
Simple TS batching function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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