๐ ThreadTS Universal





The definitive universal TypeScript library that makes true parallelism as effortless as async/await across all JavaScript ecosystems.
``
typescript
import threadts from 'threadts-universal';
// Transform any function into parallel execution with one line
const result = await threadts.run((x) => x * 2, 42);
console.log(result); // 84
// Full Array API support with parallel execution
const found = await threadts.find([1, 2, 3, 4, 5], (x) => x > 3);
console.log(found); // 4
`
โจ Features
$3
- Single-line API: await threadts.run(fn, data)
โ instant parallel execution
- Zero-Config: Intelligent defaults with infinite customization layers
- Quantum Performance: Sub-5ms overhead vs handwritten worker wrappers
$3
- Browser: Web Workers, OffscreenCanvas, Transferable Objects
- Node.js: Worker Threads, Cluster Mode, Native Addons
- Deno: Web Workers with permissions sandboxing
- Bun: Optimized worker implementation
- Identical API: Same code works everywhere
$3
- Auto-scaling Pools: From 1 to โ workers based on load
- Full Array API: map
, filter
, reduce
, reduceRight
, find
, findIndex
, some
, every
, forEach
, flatMap
, groupBy
, partition
, count
, indexOf
, lastIndexOf
, at
, slice
, concat
, range
, repeat
, unique
, uniqueBy
, chunk
, zip
- ES2023+ Immutable Methods: findLast
, findLastIndex
, toSorted
, toReversed
, withElement
, toSpliced
, groupByObject
- all methods that don't mutate the original array
- Enhanced Pipeline API: Fluent chaining with lazy evaluation
- Intermediate: map
, filter
, flatMap
, take
, skip
, chunk
, tap
, peek
, window
, unique
, distinct
, reverse
, sort
, zip
, zipWith
, interleave
, compact
, flatten
, shuffle
, sample
, dropWhile
, takeWhile
, slicePipe
, concatPipe
, rotate
, truthy
, falsy
- Terminal: reduce
, forEach
, find
, findIndex
, findLast
, findLastIndex
, some
, every
, count
, groupBy
, partition
, first
, last
, isEmpty
, sum
, average
, min
, max
, join
, includes
- Collectors: toArray()
, toSet()
, toMap()
- Progress Tracking: Real-time progress monitoring
- Intelligent Caching: Automatic result caching with @memoize
and @cache
decorators
- Priority Queues: High/Normal/Low priority execution
- Timeout & Cancellation: AbortController integration
- Decorator Suite: @parallelMethod()
, @retry()
, @rateLimit()
, @timeout()
, @debounce()
, @throttle()
, @logged()
, @cache()
, @concurrent()
, @circuitBreaker()
, @measure()
, @validate()
, @lazy()
- Custom Decorators: Utilities for creating your own decorators (createMethodDecorator
, createMethodDecoratorWithClass
, createClassDecorator
)
- Monitoring: Built-in performance monitoring, health checks, and error handling
๐ Quick Start
$3
`
bash
npm install threadts-universal
`
$3
`
typescript
import threadts from 'threadts-universal';
// Simple parallel execution
const doubled = await threadts.run((x) => x * 2, 21);
// Complex calculations
const fibonacci = await threadts.run((n) => {
if (n <= 1) return n;
let a = 0,
b = 1;
for (let i = 2; i <= n; i++) {
[a, b] = [b, a + b];
}
return b;
}, 40);
// Parallel array processing
const squares = await threadts.map([1, 2, 3, 4, 5], (x) => x * x);
// Result: [1, 4, 9, 16, 25]
// Find elements in parallel
const firstMatch = await threadts.find([1, 2, 3, 4, 5], (x) => x > 3);
// Result: 4
// Check conditions across array
const hasEven = await threadts.some([1, 3, 5, 6], (x) => x % 2 === 0);
// Result: true
// Group and partition data
const [evens, odds] = await threadts.partition(
[1, 2, 3, 4, 5],
(x) => x % 2 === 0
);
// evens: [2, 4], odds: [1, 3, 5]
// Pipeline API for chained operations
const result = await threadts
.pipe([1, 2, 3, 4, 5])
.map((x) => x * 2)
.filter((x) => x > 4)
.reduce((acc, x) => acc + x, 0)
.execute();
// Result: 24
`
$3
`
typescript
import {
parallelMethod,
memoize,
retry,
rateLimit,
timeout,
debounce,
} from 'threadts-universal';
class DataProcessor {
@parallelMethod()
async processLargeDataset(data: number[]): Promise {
return data.map((x) => x x x);
}
@parallelMethod({ timeout: 5000, priority: 'high' })
async criticalCalculation(input: ComplexData): Promise {
// Heavy computation automatically runs in worker
return heavyProcessing(input);
}
@memoize(100) // Cache up to 100 results
async expensiveComputation(input: string): Promise {
return computeResult(input);
}
@retry(3, 1000) // 3 attempts with exponential backoff
async unreliableOperation(): Promise {
await callExternalService();
}
@rateLimit(10) // Max 10 calls per second
async rateLimitedAPI(): Promise {
return fetchFromAPI();
}
}
`
๐ API Reference
$3
#### threadts.run
(fn, data?, options?): Promise
Executes a function in a worker thread.
`typescript
const result = await threadts.run(
(data: { x: number; y: number }) => data.x + data.y,
{ x: 10, y: 20 },
{
timeout: 5000,
priority: 'high',
transferable: [], // For transferable objects
}
);
`
#### threadts.parallel(tasks): Promise
Executes multiple functions in parallel.
`typescript
const results = await threadts.parallel([
{ fn: (x) => x * 2, data: 5 },
{ fn: (x) => x * 3, data: 7 },
{ fn: (x) => x * 4, data: 9 },
]);
// Results: [10, 21, 36]
`
#### threadts.map(array, fn, options?): Promise
Maps an array through a function in parallel.
`typescript
const results = await threadts.map(
[1, 2, 3, 4, 5],
(x, index) => ({ value: x * x, index }),
{ batchSize: 2 }
);
`
#### threadts.filter(array, fn, options?): Promise
Filters an array in parallel.
`typescript
const evens = await threadts.filter(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
(x) => x % 2 === 0
);
`
#### threadts.reduce(array, fn, initialValue, options?): Promise
Reduces an array in parallel (for associative operations).
`typescript
const sum = await threadts.reduce(
[1, 2, 3, 4, 5],
(acc, curr) => acc + curr,
0
);
`
#### threadts.reduceRight(array, fn, initialValue, options?): Promise
Reduces an array from right to left.
`typescript
const result = await threadts.reduceRight(
['a', 'b', 'c'],
(acc, item) => acc + item,
''
);
// Result: 'cba'
`
#### threadts.find(array, predicate, options?): Promise
Finds the first element that satisfies the predicate (processes in parallel batches).
`typescript
const found = await threadts.find([1, 2, 3, 4, 5], (x) => x > 3);
// Result: 4
`
#### threadts.findIndex(array, predicate, options?): Promise
Finds the index of the first element that satisfies the predicate.
`typescript
const index = await threadts.findIndex([1, 2, 3, 4, 5], (x) => x > 3);
// Result: 3
`
#### threadts.some(array, predicate, options?): Promise
Tests whether at least one element satisfies the predicate.
`typescript
const hasEven = await threadts.some([1, 3, 5, 6, 7], (x) => x % 2 === 0);
// Result: true
`
#### threadts.every(array, predicate, options?): Promise
Tests whether all elements satisfy the predicate.
`typescript
const allPositive = await threadts.every([1, 2, 3, 4, 5], (x) => x > 0);
// Result: true
`
#### threadts.forEach(array, fn, options?): Promise
Iterates over an array in parallel (like Array.forEach but parallel).
`typescript
await threadts.forEach([1, 2, 3], (x) => {
console.log(x);
});
`
#### threadts.flatMap(array, fn, options?): Promise
Maps each element to an array and flattens the result.
`typescript
const result = await threadts.flatMap([1, 2, 3], (x) => [x, x * 2]);
// Result: [1, 2, 2, 4, 3, 6]
`
#### threadts.groupBy(array, keyFn, options?): Promise