Comparisons
Compare ciorent usage with other concurrency libraries.
Appending streams to a file
Section titled “Appending streams to a file”Stream files sequentially to output.
import { mutex } from 'ciorent';import { streamToOutput, streamFile } from './output.ts';
const mu = mutex.init();for (let i = 0; i < 5; i++) mutex.run(mu, streamToOutput, streamFile(i));import Mutex from 'p-mutex';import { streamToOutput, streamFile } from './output.ts';
const mu = new Mutex();for (let i = 0; i < 5; i++) mu.withLock(() => streamToOutput(streamFile(i)));import { Mutex } from 'async-mutex';import { streamToOutput, streamFile } from './output.ts';
const mu = new Mutex();for (let i = 0; i < 5; i++) mu.runExclusive(() => streamToOutput(streamFile(i)));import pLimit from 'p-limit';import { streamToOutput, streamFile } from './output.ts';
const limit = pLimit(1);for (let i = 0; i < 5; i++) limit(streamToOutput, streamFile(i));import type { ReadStream } from 'node:fs';import { streamToOutput, streamFile } from './output.ts';
let lock = Promise.resolve();const lockedStreamToOutput = async (inputStream: ReadStream) => { try { await lock; } finally { return streamToOutput(inputStream); }}for (let i = 0; i < 5; i++) lock = lockedStreamToOutput(streamFile(i));Get file hashes concurrently
Section titled “Get file hashes concurrently”Read file content and return the hashes, need to limit concurrent file reads to avoid issues such as too many file handles.
import { readFile, hash } from './file.ts';import { semaphore } from 'ciorent';
const getHash = async (path: string) => hash(await readFile(path));
export const getHashes = (paths: string[]) => { // No limit when not needed if (paths.length < 10) return Promise.all(paths.map(getHash));
const sem = semaphore.init(10, paths.length - 10); return Promise.all( paths.map((path) => semaphore.run(sem, getHash, path)) );}import { readFile, hash } from './file.ts';import { limitFunction } from 'p-limit';
const getHash = async (path: string) => hash(await readFile(path));
export const getHashes = (paths: string[]) => { // No limit when not needed if (paths.length < 10) return Promise.all(paths.map(getHash));
return Promise.all( paths.map(limitFunction(getHash, { concurrency: 10 })) );}