Split your list into batches of a fixed size. This is the most common mode for database queries and API requests with batch size limits.
type SplitMode = 'items_per_group';
How it works:
Divide items into groups of exactly N items
The last batch may contain fewer items if the total doesn’t divide evenly
Order is preserved exactly as entered
Example use case:
1
SQL IN clause with 500-item limit
You have 2,847 transaction IDs to query, but your database has a 500-item limit for IN clauses.Set mode to Items per batch with value 500. You’ll get 6 batches: five with 500 items and one with 347 items.
The implementation uses array slicing for optimal performance:
function splitByItemsPerGroup(items: string[], perGroup: number): SplitGroup[] { const chunks: string[][] = []; for (let i = 0; i < items.length; i += perGroup) { chunks.push(items.slice(i, i + perGroup)); } return buildGroups(chunks);}
Split based on character count rather than item count. Useful when you have character limits for API payloads, text processing systems, or string length constraints.
type SplitMode = 'max_chars_per_group';
How it works:
Pack as many items as possible into each batch without exceeding the character limit
Accounts for newline separators between items (1 character each)
Ensures no batch exceeds your specified character budget
Single items longer than the limit will create their own batch
Example use case:
1
API with 10,000-character payload limit
You’re sending item lists to an API endpoint that accepts a maximum 10KB text payload.Set mode to Max characters per batch with value 10000. SplitBox packs items efficiently while respecting the character limit.
The character count includes separator characters. For newline-delimited output, each separator adds 1 character to the total.
Specify how many batches you want, and SplitBox distributes items as evenly as possible across them.
type SplitMode = 'target_group_count';
How it works:
Calculates optimal batch size to create exactly N batches
Distributes remainder items across the first batches (one extra item each)
All batches will have similar sizes (differ by at most 1 item)
Example use case:
1
Parallel processing across 8 workers
You have 1,523 items to process in parallel across 8 worker threads.Set mode to Target batch count with value 8. SplitBox creates 8 batches: seven with 191 items and one with 186 items.
Implementation details:
function splitByTargetGroupCount(items: string[], targetGroupCount: number): SplitGroup[] { if (items.length === 0) return []; const actualGroupCount = Math.min(targetGroupCount, items.length); const baseSize = Math.floor(items.length / actualGroupCount); const remainder = items.length % actualGroupCount; const chunks: string[][] = []; let cursor = 0; for (let i = 0; i < actualGroupCount; i += 1) { const size = baseSize + (i < remainder ? 1 : 0); chunks.push(items.slice(cursor, cursor + size)); cursor += size; } return buildGroups(chunks);}
If you request more batches than items, SplitBox creates one batch per item (actual count = min(target, item count)).
function assertPositiveInteger(value: number, fieldName: string): void { if (!Number.isInteger(value) || value < 1) { throw new Error(`${fieldName} must be a positive integer`); }}
Non-integer or negative values will cause the split operation to fail with a validation error.