A fast way to iterate, sync or async, over array likes, stream and async iterables. It provides fluent api operations so you can easily chain many operations.

Fluent-iterable are strongly focused on performance. Check it out in our benchmark!; Here a some results we got comparing to another similar options on node 22:
| Library | Ops/sec | Margin | Ran samples |
|---|---|---|---|
| fluent | 494 | ±1.43% | 91 |
| iterare | 436 | ±1.10% | 92 |
| iter-ops | 376 | ±0.21% | 91 |
| native builtin iterator | 326 | ±0.21% | 92 |
| iter-tools | 243 | ±0.17% | 88 |
| rxjs | 197 | ±0.49% | 83 |
| native generators | 155 | ±0.83% | 81 |
| native array operations chain | 63.71 | ±2.79% | 66 |
Notice that what we call native builtin ierator is the iterator helper, recently released in the NodeJs 22, so due to some strategies we use, we can achieve a performance even better than the helper implemented in the V8 Engine! You can check the benchmark code here
When you're dealing with complex operations involving lists with multiple items, you can fall in the need of transform, filter, perform a flat map or even take just some items from that list. In cases like that, if you're dealing with a pure array, one solution is to simply do something like this:
const flattedList = [];
const list2 = list
.filter((x) => filter1(x))
.map((X) => transform1(x))
.filter((x) => filter2(x))
.forEach((x) => flattedList.push(...x));
const result = flattedList
.filter((x) => filter3(x))
.slice(0, 10)
.map((x) => transform2(x));
This code looks fluent and easy to read, but it is severe to performance. That's because each operation do a complete iteration over the array, generating a new one! It can cause serious memory and cpu consumption, so, such practice is not encouraged. So, to solve this, you can write an equivalent code which will solve everything in with two chained loops. This will give you the best performance possible, but can result in a code harder to maintain. And that's where fluent-iterable comes in!
With fluent-iterable, you can do the same operation with the following code:
const result = fluent(list)
.filter((x) => filter1(x))
.map((X) => transform1(x))
.filter((x) => filter2(x))
.flatMap()
.filter((x) => filter3(x))
.take(10)
.map((x) => transform2(x))
.toArray();
Pretty simple, right? With this code, you'll do exactly two chained loops, exactly as the vanilla solution described above! fluent-iterable takes advantage of the Iterable and AsyncIterable contracts to achieve this, but it goes beyond. It uses a special library called augmentative-iterables that we developed as the core engine of the iterations. This library is focused exclusively in performance, so, with it, we achieved a processing time very close to the vanilla solution above! Comparing to iterations using for of, the native way to iterate over iterables of JavaScript, we achieved a result 50% faster!
No.
Although you can manipulate Iterables in a similar way, rxjs have some key different behaviors and a different purpose overall. In summary, we can use rxjs as what themselves define it in their website:
Think of RxJS as Lodash for events.
That's it. Rxjs is focused primarily in event handling. Over that, some key differences can be pointed out:
So, as you see, regardless some similarities, there're some pretty important differences between them and those are libraries focused on quite different problems.
fluent-iterable have some neat operations already implements. If you want to Click here for the Full API Reference.
ECMAScript introduced support for iterables and generator functions with version ES6 and their asynchronous counterparts with version ES2018. It has introduced an abstraction over sequential iterators (arrays, maps, generators, etc), enabling us to implement solutions regardless of the actual type of the iterable collection. It is especially powerful when using in tandem with generator functions to avoid storing all items in memory when its avoidable. The API provided by fluent-iterable reads the elements of the underlying iterable only when needed and stops reading elements as soon as the result is determined.
To get started with the fluent API, you need to translate the iterable (can be any object with Symbol iterator or asyncIterator defined) into either a FluentIterable using fluent() or a FluentAsyncIterable using fluentAsync().
import fetch from 'node-fetch';
import {
fluent,
fluentAsync,
FluentIterable,
FluentAsyncIterable,
} from '@codibre/fluent-iterable';
const iterableOfArray: FluentIterable<number> = fluent([3, 1, 8, 6, 9, 2]);
function* naiveFibonacci(): Iterable<number> {
yield 0;
yield 1;
let x = 0;
let y = 1;
while (true) {
y = x + y;
x = y - x;
yield y;
}
}
const iterableOfGenerator = fluent(naiveFibonacci());
async function* emails(): AsyncIterable<string> {
let page = 1;
while (true) {
const res = await fetch(`https://reqres.in/api/users?page=${page}`);
if (!res.ok) {
break;
}
yield* (await res.json()).data.map((user: { email: string }) => user.email);
}
}
const asyncIterableOfEmails = fluentAsync(emails());
Once you have an instance of a fluent iterable, you can start chaining any of the supported operations to express what you need, like:
...
interface ChatMessage {
id: number;
from: string;
to: string;
body: string;
}
...
function getAllMessages(iterable: FluentAsyncIterable<ChatMessage>): FluentAsyncIterable<string> {
return iterable.map(chatMessage => chatMessage.body);
}
function getAllUsers(iterable: FluentAsyncIterable<ChatMessage>): FluentAsyncIterable<string> {
return iterable
.flatMap(chatMessage => [ chatMessage.from, chatMessage.to ]) // convert the message entries into arrays of sender and recipient and flatten them
.distinct(); // yield the users only once
}
function getNumberOfUsers(iterable: FluentAsyncIterable<ChatMessage>): Promise<number> {
return getAllUsers(iterable).count();
}
async function getMostActiveUser(iterable: FluentAsyncIterable<ChatMessage>): Promise<string | undefined> {
const maxGroup = await iterable
.group(chatMessage => chatMessage.from) // group the messages by their sender
.max(chatMessage => chatMessage.values.count()); // find one of the groups which has the most messages
return maxGroup?.key;
}
async function hasUserSentEmptyMessage(iterable: FluentAsyncIterable<ChatMessage>, user: string): Promise<bool> {
return await iterable
.any(chatMessage => chatMessage.from === user && chatMessage.body.length === 0); // will stop reading elements as soon as found one which satisfying the condition
}
async function createBackupSequential(iterable: FluentAsyncIterable<ChatMessage>): Promise<void> {
await iterable
.execute(chatMessage => console.log(`Backing up message ${chatMessage.id}.`)) // log progress w/o modifying the iterable
.forEach(chatMessage => fetch(BACKUP_URL, { // execute the asynchronous backup operation against all elements one-by-one
method: 'post',
body: JSON.stringify(chatMessage),
headers: { 'Content-Type': 'application/json' },
}));
}
async function createBackupParallel(iterable: FluentAsyncIterable<ChatMessage>): Promise<void> {
await iterable
.execute(chatMessage => console.log(`Backing up message ${chatMessage.id}.`)) // log progress w/o modifying the iterable
.map(chatMessage => {
const result = fetch(BACKUP_URL, { // translate all elements into a promise of their asynchronous backup operation
method: 'post',
body: JSON.stringify(chatMessage),
headers: { 'Content-Type': 'application/json' },
}).then(x => [x]);
return fluentAsync(result);
})
// Joins everything in parallel, generating an AsyncIterable with the results in the order of what yielded first
.flatMerge(
(error) => console.log(error) // This callback will be called whenever some of the fetch calls throws an error
)
.last();
}
async function createBackupParallelV2(iterable: FluentAsyncIterable<ChatMessage>): Promise<Response[]> {
return iterable
.execute(chatMessage => console.log(`Backing up message ${chatMessage.id}.`)) // log progress w/o modifying the iterable
.waitAll(chatMessage => fetch(BACKUP_URL, { // translate all elements into a promise of their asynchronous backup operation
method: 'post',
body: JSON.stringify(chatMessage),
headers: { 'Content-Type': 'application/json' },
}));
}
You can see a list of many advanced examples for fluent clicking here!!
import { fluent } from '@codibre/fluent-iterable';
function* naiveFibonacci(): Iterable<number> {
yield 0;
yield 1;
let x = 0;
let y = 1;
while (true) {
y = x + y;
x = y - x;
yield y;
}
}
// What is the sum of the first 100 fibonacci numbers?
console.log(
fluent(naiveFibonacci())
.takeWhile((n) => n < 100)
.sum(),
);
// How many fibonacci numbers are there between 1K and 1M?
console.log(
fluent(naiveFibonacci())
.skipWhile((n) => n < 1000)
.takeWhile((n) => n < 1000000)
.count(),
);
// What are the 10th to 20th fibonacci numbers?
console.log(fluent(naiveFibonacci()).skip(9).take(10).toArray());
// What are the halves of the first 20 even fibonacci numbers?
console.log(
fluent(naiveFibonacci())
.filter((n) => n % 2 === 0)
.take(20)
.map((n) => n / 2)
.toArray(),
);
``` typescript import { fluent } from '@codibre/fluent-iterable';
enum Gender { Male = 'Male', Female = 'Female', NonBinary = 'NonBinary', }
interface Person { name: string; gender?: Gender; emails: string[]; }
const people: Person[] = [ { name: 'Adam', gender: Gender.Male, emails: ['adam@adam.com'], }, { name: 'Christine', gender: Gender.Female, emails: [], }, { name: 'Sebastian', emails: ['sebastian@sebastian.com', 'sebastian@corp.com'], }, { name: 'Alex', gender: Gender.Female, emails: ['alex@alex.com'], }, ];
// Log all the names! for (const name of fluent(people).map((p) => p.name)) { console.log(name); }
// Log all the emails! console.log( fluent(people) .flatten((p) => p.emails) .toArray(), );
// Are there any persons without gender specified? console.log(fluent(people).any((p) => !p.gender));
// Are all the persons have at least one email? console.log(fluent(people).all((p) => p.emails.length > 0));
// Who is the last
$ claude mcp add fluent-iterable \
-- python -m otcore.mcp_server <graph>