Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
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
Unblock your JavaScript with non-blocking array methods!
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.
# Using npm
npm install unjam
# Using yarn
yarn add unjam
# Using pnpm
pnpm add unjam
For a full API reference and detailed examples, check out the Documentation.
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
});
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
Check out how Unjam’s cooperative multitasking keeps your app smooth and responsive:
In non-cooperative execution, long-running tasks block the main thread, leading to unresponsive UIs and poor user experiences.
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.
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
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.
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.
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.
Contributions are welcome! Please open an issue or submit a pull request to help improve Unjam.
This project is licensed under the MIT License - see the LICENSE file for details.
FAQs
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
We found that unjam demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.