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

scale-array

v1.0.1

Published

an array which scales (to disk)

Downloads

14

Readme

⚖️ ScaleArray

ScaleArray is a Node.js module that allows you to handle large arrays efficiently by storing them in memory and flushing items to disk as json when they reach a certain length.

Because ScaleArray presents it's API as a regular array, you can use familiar array methods like map, filter, reduce, and forEach to process and transform large datasets with ease.

File I/O is synchronous, so you do not need to worry about race conditions or asynchronous callbacks; essentially:

  • when you push() items onto the array, if the array grows larger than maxSize, it will the contents of the array to disk before continuing.

  • when you pop() items from the array, it will load the contents of the array from disk if it is not already in memory.

this means you can work with large datasets as if they were in memory, without worrying about memory constraints.

Installation

npm install scale-array

Features

  • In-Memory Array Handling: Manipulate large arrays as if they are in memory.
  • Automatic Disk Flushing: Automatically flushes arrays to disk when they exceed a specified size.
  • Iterable and Transformable: Iterate, map, filter, reduce, and flatten large datasets easily.
  • Customizable: Specify the maximum in-memory size, file paths, and more.

Usage

Basic Example

const ScalableArray = require('scalable-array');

const options = {
  name: 'myArray',
  writePath: './data',
  maxSize: 100 // Flush to disk after 100 items
};

const array = new ScalableArray(options);

// Add some items
array.push({ item: 1 });
array.push({ item: 2 });

// Automatically flushes when maxSize is reached
array.push([{ item: 3 }, { item: 4 }, { item: 5 }]);

console.log(`Array length: ${array.length}`); // 5

Iterate Over Items

for (const item of array) {
  console.log(item);
}

Transform Data

// Map items to a new array
const mappedArray = array.map(item => ({ ...item, mapped: true }));
for (const item of mappedArray) {
  console.log(item); // { item: 1, mapped: true }, etc.
}

// Filter items into a new array
const filteredArray = array.filter(item => item.item % 2 === 0);
for (const item of filteredArray) {
  console.log(item); // { item: 2 }, etc.
}

Reduce Data

const sum = array.reduce((acc, item) => acc + item.item, 0);
console.log(`Sum of items: ${sum}`); // 15

Flatten Nested Arrays

const nestedArray = new ScalableArray({ writePath: './data', maxSize: 2 });
nestedArray.push([1, [2, [3, 4]]]);
const flatArray = nestedArray.flat(2);
console.log(flatArray.toArray()); // [1, 2, 3, 4]

API

Constructor

const array = new ScalableArray(options);
  • options.name (string): The name of the ScalableArray instance.
  • options.writePath (string): The path to write the JSON files to.
  • options.maxSize (number): The maximum size of the in-memory array before flushing.

Methods

  • push(item): Adds an item (or an array of items) to the array, flushing to disk if necessary.
  • shift(): Removes and returns the first item from the array.
  • flush(force = false): Flushes the current array to disk.
  • clear(): Clears the array and deletes all files.
  • forEach(callback): Applies a function to each item in the array.
  • map(callback, name): Maps each item in the array to a new ScalableArray.
  • filter(callback, name): Filters items into a new ScalableArray.
  • reduce(callback, initialValue, name): Reduces the array to a single value.
  • flat(depth = 1, name): Flattens nested arrays into a new ScalableArray.
  • toArray(consumeOriginal = false): Collects all values in the array and returns a full array.

Properties

  • length: The total number of items in the array.
  • files: The list of file paths where data has been flushed.
  • writePath: The current write path.
  • name: The name of the ScalableArray instance.
  • maxSize: The maximum size of the in-memory array before flushing.

Static Methods

  • ScalableArray.memCheck(percent = 0.75): Checks memory usage and triggers garbage collection if usage exceeds the specified percentage.

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

MIT License