Skip to content

Instantly share code, notes, and snippets.

@szanata
Last active September 19, 2018 17:50
Show Gist options
  • Save szanata/4299564215c9867d0b3efca13a10af40 to your computer and use it in GitHub Desktop.
Save szanata/4299564215c9867d0b3efca13a10af40 to your computer and use it in GitHub Desktop.
Batch splitter
const sizeLimit = 1000000;
const lengthLimit = 500;
const getSize = obj => Buffer.byteLength( JSON.stringify( obj ) );
const isUnderLimits = batch => batch.length <= lengthLimit && getSize( batch ) <= sizeLimit;
module.exports = records => records.reduce( ( batches, record ) => {
if ( isUnderLimits( [].concat( batches[0], record ) ) ) {
batches[0].push( record );
} else {
batches.unshift( [ record ] );
}
return batches;
}, [] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment