npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mangosteen/batch-utils

v1.0.0

Published

Batch up iterables into fixed-size arrays.

Downloads

3

Readme

batch-utils

Take an iterable (such as an array) of values, and batch the values into multiple fixed-length arrays.

Both sync and async iterable generators are available.

What is it for?

Imagine needing to parse a huge CSV file and loading it into a database. The file is too big to fit into memory. You could just read the file line by line, but sending individual INSERTs to a SQL database is slow.

@mangosteen/batch-utils allows you to to batch the entries in preparation before executing a batch SQL insert. This provides significant performance speed-up.

Installation

With npm do:

$ npm install @mangosteen/batch-utils

Basic Usage

import { batchItemsAsync } from '@mangosteen/batch-utils';

(async () => {
    const values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    const batches = batchItemsAsync(values, 3);

    for await (const batch of batches) {
        console.log(batch);
        // console.log(typeof batch); // Array<string>
    }
})();

This code will output the following arrays:

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]

Advanced Usage

import fs from 'fs';
import { lineByLine } from '@mangosteen/line-by-line';
import { batchItemsAsync } from '@mangosteen/batch-utils';

(async () => {
    const inputStream = fs.createReadStream('./shakespeare.txt');
    const lines = lineByLine(inputStream);
    const lineBatches = batchItemsAsync(lines, 100);

    for await (const batch of lineBatches) {
        // sql.batchInsert(batch);
        console.log(typeof batch); // Array<string>
    }
})();

API

batchItemsAsync(items, desiredLength)

Generates arrays of length desiredLength containing the values from items. The final array may be shorter than desiredLength if there are not enough values in items.

items: AsyncIterable<T> : Iterable of items to batch up into fixed-size arrays.

desiredLength: number : The desired length of the generated arrays.


forEachBatchAsync(items, desiredLength, onBatch)

Performs the specified action for each array generated from items iterable.

The generated arrays will be of length desiredLength, except for the last array which may be shorter.

items: AsyncIterable<T> : Iterable of items to batch up into fixed-size arrays.

desiredLength: number : The desired length of the generated arrays.

onBatch: (batch: T[], batchIndex: number) => Promise<void> : Action to perform for each array.


batchItemsSync(items, desiredLength)

Generates arrays of length desiredLength containing the values from items.

The final array may be shorter than desiredLength if there are not enough values in items.

items: Iterable<T> : Iterable of items to batch up into fixed-size arrays.

desiredLength: number : The desired length of the generated arrays.


forEachBatchSync(items, desiredLength, onBatch)

Performs the specified action for each array generated from items iterable.

The generated arrays will be of length desiredLength, except for the last array which may be shorter.

items: Iterable<T> : Iterable of items to batch up into fixed-size arrays.

desiredLength: number : The desired length of the generated arrays.

onBatch: (batch: T[], batchIndex: number) => void : Action to perform for each array.