Guide: A more stable way for mass save operations than Parse.Object.saveAll

Parse has Parse.Object.saveAll() method for saving objects in a single api call.

But it has two limitations - > first being its sequential nature, which increases execution time and blocks the rest of the code, and second the strain it puts on the server by trying to save everything in a single call (especially if the objects are heavy).

Therefore a more stable way would be to save in batches using Promise.all() with a custom function like below:

async function saveAllInBatches(objects, batchSize, options) {
  const batches = [];

  for (let i = 0; i < objects.length; i += batchSize) {
    const batch = objects.slice(i, i + batchSize);
    const promises = batch.map((object) => object.save(null, options));
    batches.push(promises);
  }

  for (const batch of batches) {
    await Promise.all(batch);
  }
}

module.exports = { saveAllInBatches };

This function accepts an array of parse objects to be saved, batch size, and an optional options objects. And it saves the batches in parallel increasing the speed of operations and distributing the strain over 1000 calls;

    saveAllInBatches(rAccs, 1000, { useMasterKey: true });

Overall if you have 1000 000 objects to be saved, the Parse.Object.saveAll function would save them in 1 api call, while this custom function would make 1000 api calls. Yet make it concurrently and with a lower risk of timeouts or memory outages for your server.

Feel free to comment.

2 Likes