Skip to content

Instantly share code, notes, and snippets.

@0xdeepmehta
Created December 10, 2022 09:51
Show Gist options
  • Save 0xdeepmehta/6a53e20ace52535c6bd43c0bbd04512b to your computer and use it in GitHub Desktop.
Save 0xdeepmehta/6a53e20ace52535c6bd43c0bbd04512b to your computer and use it in GitHub Desktop.
// Define the size of each chunk
const chunkSize = 1000;
// Create a function to iterate over the items in chunks
async function iterateInChunks(items: any[], callback: (chunk: any[]) => void) {
// Calculate the number of chunks
const numChunks = Math.ceil(items.length / chunkSize);
// Create an array to hold the promises for the async operations
const promises = [];
// Iterate over each chunk
for (let i = 0; i < numChunks; i++) {
// Calculate the start and end indices for the current chunk
const startIndex = i * chunkSize;
const endIndex = startIndex + chunkSize;
// Get the items for the current chunk
const chunk = items.slice(startIndex, endIndex);
// Create a new promise to process the current chunk
const promise = callback(chunk);
// Add the promise to the array
promises.push(promise);
}
// Wait for all of the promises to complete
await Promise.all(promises);
}
// Define the array of items to iterate over
const items = [/* 750k items here */];
// Iterate over the items in chunks and process each chunk in parallel
iterateInChunks(items, chunk => {
return new Promise(resolve => {
// Process the items in the current chunk
// ...
// Resolve the promise when the chunk has been processed
resolve();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment