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

unjam

v0.3.2

Published

Unjam is a JavaScript and TypeScript library offering non-blocking versions of common array methods like forEach, map, and filter. Designed for cooperative multitasking, Unjam keeps your application responsive, even with large data processing tasks, by pr

Downloads

487

Readme

Unjam

Unblock your JavaScript with non-blocking array methods!

npm version Static Badge License: MIT

🚀 Overview

Unjam is a JavaScript/TypeScript library that provides non-blocking, cooperative multitasking versions of common array methods like forEach, map, filter, and more. By using Unjam, you can process large datasets without freezing the main thread, ensuring smooth and responsive user experiences.

✨ Features

  • Non-blocking operations: Keep your UI responsive even when working with large datasets.
  • Zero dependencies: Lightweight and efficient, with no extra overhead.
  • Familiar API: Uses the same method signatures as standard array methods.
  • Tree-shaking support: Import only what you need for optimal bundle size.
  • TypeScript support: Fully typed for a better development experience.
  • Lightweight: Minimal overhead with maximum benefits.

📦 Installation

# Using npm
npm install unjam

# Using yarn
yarn add unjam

# Using pnpm
pnpm add unjam

📚 Documentation

For a full API reference and detailed examples, check out the Documentation.

🛠️ Usage

Import the methods you need from Unjam and use them just like the standard array methods, but with the added benefit of non-blocking execution. Thanks to tree-shaking support, importing directly from the module ensures that only the code you use gets included in your bundle.

import { forEach, map, filter } from 'unjam';

const largeArray = [...]; // Your large dataset

// Non-blocking forEach
await forEach(largeArray, (item) => {
  // Your logic here
});

// Non-blocking map
const result = await map(largeArray, (item) => {
  // Your transformation here
});

// Non-blocking filter
const filtered = await filter(largeArray, (item) => {
  // Your condition here
});

🌳 Tree-Shaking Support

Unjam is designed with tree-shaking in mind. By importing methods directly from the module, modern bundlers like Webpack and Rollup can eliminate unused code from your final bundle, optimizing load times and performance.

import { map } from "unjam/map"; // Only imports the 'map' function

📊 Performance Comparison

Check out how Unjam’s cooperative multitasking keeps your app smooth and responsive:

Non-Cooperative Execution

Non-Cooperative Execution

In non-cooperative execution, long-running tasks block the main thread, leading to unresponsive UIs and poor user experiences.

Cooperative Execution with Unjam

Cooperative Execution with Unjam

With Unjam's cooperative execution, tasks are broken into manageable chunks, allowing the main thread to remain responsive and the UI to stay smooth.

These graphs illustrate how Unjam prevents the main thread from being blocked during intensive array operations, ensuring your application stays responsive.

🔄 Advanced Usage and Patterns

Combining Methods

Chain methods like map and filter with Unjam for more complex data processing.

import { map, filter } from "unjam";

const largeArray = Array.from({ length: 10_000_000 }, (_, i) => i);

const squared = map(largeArray, (num) => {
  return num * num;
});
const filtered = await filter(squared, (num) => {
  return num % 2 === 0;
});

// or

const filtered2 = await filter(map(largeArray, (num) => {
  return num * num
}), (num) => num % 2 === 0)

console.log(filtered);
console.log(filtered2

Creating Custom Algorithms

Unjam includes a cooperate function to build custom, cooperative algorithms. Let’s create a function that squares and filters an array in a cooperative-friendly way.

import { map, filter, cooperate } from "unjam";

const largeArray = Array.from({ length: 100000 }, (_, i) => i);

const squareAndFilter = async (array) =>
  await cooperate(async () => {
    const squared = await map(array, (num) => num * num);
    const filtered = filter(squared, (num) => num % 2 === 0);
    return filtered;
  });

console.log(await squareAndFilter(largeArray));

You’re not limited to using only cooperative methods inside cooperate; create custom logic to control when to yield back to the event loop:

import { cooperate } from "unjam";

// Yield back control 3 times.
await cooperate(async (handoff) => {
  await handoff();
  await handoff();
  await handoff();
});

Behind the scenes, all Unjam methods are powered by the cooperate function, keeping execution cooperative and non-blocking.

💬 FAQ

Q: How does Unjam differ from using Web Workers?
A: Unjam operates on the main thread but breaks tasks into non-blocking chunks, unlike the multi-threaded approach of Web Workers.

Q: Is Unjam suitable for real-time applications?
A: Yes, Unjam is designed to keep the main thread responsive, making it ideal for real-time applications handling large datasets.

⚠️ Limitations & Considerations

Unjam is optimized for large datasets with similar processing times per item. However, native methods may offer better performance in some cases. Ensure that chunking large tasks aligns with your application’s performance objectives.

🤝 Contributing

Contributions are welcome! Please open an issue or submit a pull request to help improve Unjam.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.