Processing an array in batches using generators

Generators in JavaScript are something that looks useful on the surface but I have really never found much use for them in my work. However one such use case I came across was processing a large mount of data in an array in batches. I needed to process and push data to a DynamoDB table, but I had to batch it to avoid slamming the table with write operations.

As is turned out, using generators massively simplified the batching code. Here is a simplified example:


1
2
3
4
5
6
7
8
9
10
11
// define the generator function that yields
// N items at a time from the provided array
function* getBatch(records, batchsize = 5) {
    while (records.length) {
        yield records.splice(0, batchsize);
    }
}

for (let batch of getBatch(records)) {
    // do something with 5 items at a time
}

Or another example that sends the data somewhere asynchronously:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import axios from "axios";

// define the generator function that yields
// N items at a time from the provided array
function* getBatch(records, batchsize = 5) {
    while (records.length) {
        yield records.splice(0, batchsize);
    }
}

async function uploadDataSomewhere(data) {
    return axios.post('/some/url', { data });
}

async function run(records) {
    for (let batch of getBatch(records)) {
        // do something with 5 items at a time
        // for example upload the data somewhere:
        await Promise.all(batch.map(record => uploadDataSomewhere(record)));
    }
}

let somedata = [1, 2, 3, 4, 5, 6];

run(somedata);

Photo by Gem & Lauris RK on Unsplash

Posted toJavascript