# chunkify

> Split an iterable into evenly sized chunks

Latest version **5.0.0** (published 2023-12-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install chunkify
pnpm add chunkify
yarn add chunkify
bun add chunkify
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2023-12-27 |
| First published | 2015-09-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 5.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | chunkify, chunk, chunks, chunked, chunking, array, iterable, iterator, generator, set, map, parts, split, size, partition, divide, segment, batch, slice, subarray, group, allocate, dissect, segregate, separate, section |

## Links

- npm: https://www.npmjs.com/package/chunkify
- Repository: https://github.com/sindresorhus/chunkify
- Homepage: https://github.com/sindresorhus/chunkify#readme
- Issues: https://github.com/sindresorhus/chunkify/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/chunkify

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 5.0.0 (latest) — 2023-12-27
- 4.0.0 — 2016-01-26
- 3.0.6 — 2016-01-18
- 3.0.5 — 2015-10-05
- 3.0.4 — 2015-10-04
- 3.0.3 — 2015-10-04
- 3.0.1 — 2015-10-04
- 3.0.0 — 2015-10-04
- 2.1.1 — 2015-09-27
- 2.1.0 — 2015-09-27
- 2.0.4 — 2015-09-22
- 2.0.3 — 2015-09-22
- 2.0.2 — 2015-09-22
- 2.0.1 — 2015-09-22
- 2.0.0 — 2015-09-22
- … 6 more at https://npm.io/package/chunkify/versions

## README

# chunkify

> Split an iterable into evenly sized chunks

## Install

```sh
npm install chunkify
```

## Usage

```js
import chunkify from 'chunkify';

console.log([...chunkify([1, 2, 3, 4], 2)]);
//=> [[1, 2], [3, 4]]

console.log([...chunkify([1, 2, 3, 4], 3)]);
//=> [[1, 2, 3], [4]]
```

## API

### chunkify(iterable, chunkSize)

Returns an iterable with the chunks. The last chunk could be smaller.

#### iterable

Type: `Iterable` *(for example, `Array`)*

The iterable to chunkify.

#### chunkSize

Type: `number` *(integer)*\
Minimum: `1`

The size of the chunks.

## Use-cases

### Batch processing

When dealing with large datasets, breaking data into manageable chunks can optimize the batch processing tasks.

```js
import chunkify from 'chunkify';

const largeDataSet = [...Array(1000).keys()];
const chunkedData = chunkify(largeDataSet, 50);

for (const chunk of chunkedData) {
	processBatch(chunk);
}
```

### Parallel processing

Dividing data into chunks can be useful in parallel processing to distribute workload evenly across different threads or workers.

```js
import {Worker} from 'node:worker_threads';
import chunkify from 'chunkify';

const data = [/* some large dataset */];
const chunkedData = chunkify(data, 20);

for (const [index, chunk] of chunkedData.entries()) {
	const worker = new Worker('./worker.js', {
		workerData: {
			chunk,
			index
		}
	});
}
```

### Network requests

Splitting a large number of network requests into chunks can help in managing the load on the network and preventing rate limiting.

```js
import chunkify from 'chunkify';

const urls = [/* Array of URLs */];

const chunkedUrls = chunkify(urls, 10);

for (const chunk of chunkedUrls) {
	await Promise.all(chunk.map(url => fetch(url)));
}
```

---
_Source: https://npm.io/package/chunkify · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
