GithubHelp home page GithubHelp logo

sindresorhus / chunkify Goto Github PK

View Code? Open in Web Editor NEW
152.0 3.0 7.0 10 KB

Split an iterable into evenly sized chunks

License: MIT License

JavaScript 83.62% TypeScript 16.38%
array-chunk array array-manipulations javascript npm-package

chunkify's Introduction

chunkify

Split an iterable into evenly sized chunks

Install

npm install chunkify

Usage

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.

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.

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.

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)));
}

chunkify's People

Contributors

jajaperson avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

chunkify's Issues

Make fn return a promise?

I was just wondering if there is a benefit to making the chunkify function return a Promise? I am thinking of a use case where we pass a large iterable data? I'm sorry if this question is a silly one. ๐Ÿ˜„

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.