@@ -342,6 +342,103 @@ const { data, stats } = await beeThreads.turbo(arr).mapWithStats(x => x * x)
342342console .log (stats .speedupRatio ) // "7.2x"
343343```
344344
345+ ## 🚀 File Workers - External Files with ` require() ` Access
346+
347+ When you need workers to access ** external modules** , ** database connections** , or ** file system** — use file workers.
348+
349+ ### Basic Usage
350+
351+ ``` js
352+ // workers/process-user.js
353+ const db = require (' ./database' )
354+ const cache = require (' ./cache' )
355+
356+ module .exports = async function (userId ) {
357+ const user = await db .findUser (userId)
358+ return { ... user, cached: cache .get (userId) }
359+ }
360+
361+ // main.js
362+ const { beeThreads } = require (' bee-threads' )
363+
364+ const user = await beeThreads .worker (' ./workers/process-user.js' )(123 )
365+ ```
366+
367+ ### Type-Safe Workers (TypeScript)
368+
369+ ``` ts
370+ // workers/find-user.ts
371+ import { db } from ' ../database'
372+ export default async function (id : number ): Promise <User > {
373+ return db .query (' SELECT * FROM users WHERE id = ?' , [id ])
374+ }
375+
376+ // main.ts
377+ import type findUser from ' ./workers/find-user'
378+ const user = await beeThreads .worker <typeof findUser >(' ./workers/find-user' )(123 )
379+ // ^User ^number
380+ ```
381+
382+ ### 🔥 Turbo Mode for File Workers
383+
384+ Process large arrays with file workers across ** ALL CPU cores** :
385+
386+ ```
387+ ┌──────────────────────────────────────────────────────────────────────┐
388+ │ beeThreads.worker('./process-chunk.js').turbo(users, { workers: 4 })│
389+ └──────────────────────────────────────────────────────────────────────┘
390+ │
391+ ┌──────────┴──────────┐
392+ │ SPLIT INTO CHUNKS │
393+ └──────────┬──────────┘
394+ │
395+ ┌─────────────────────┼─────────────────────┐
396+ ▼ ▼ ▼
397+ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
398+ │ Worker 1 │ │ Worker 2 │ │ Worker 3 │
399+ │ [u1,u2,u3] │ │ [u4,u5,u6] │ │ [u7,u8,u9] │
400+ │ require DB │ │ require DB │ │ require DB │
401+ └─────────────┘ └─────────────┘ └─────────────┘
402+ │ │ │
403+ └─────────────────────┼─────────────────────┘
404+ ▼
405+ ┌──────────────────────┐
406+ │ MERGE (order kept) │
407+ │ [r1,r2...r9] │
408+ └──────────────────────┘
409+ ```
410+
411+ ``` js
412+ // workers/process-chunk.js
413+ const db = require (' ./database' )
414+ const calculateScore = require (' ./score' )
415+
416+ module .exports = async function (users ) {
417+ return Promise .all (
418+ users .map (async user => ({
419+ ... user,
420+ score: await calculateScore (user),
421+ dbData: await db .fetch (user .id ),
422+ }))
423+ )
424+ }
425+
426+ // main.js - Process 10,000 users across 8 workers
427+ const results = await beeThreads .worker (' ./workers/process-chunk.js' ).turbo (users, { workers: 8 })
428+ ```
429+
430+ ### When to Use
431+
432+ | Need | Use |
433+ | ------| -----|
434+ | Pure computation | ` bee() ` or ` turbo() ` |
435+ | Database/Redis | ` worker().turbo() ` |
436+ | External files/modules | ` worker().turbo() ` |
437+ | File system operations | ` worker().turbo() ` |
438+ | Third-party libraries | ` worker().turbo() ` |
439+
440+ ---
441+
345442## Request Coalescing
346443
347444Prevents duplicate simultaneous calls from running multiple times. When the same function with identical arguments is called while a previous call is in-flight, subsequent calls share the same Promise.
@@ -497,6 +594,9 @@ const stream = beeThreads
497594- ** Large array processing** (turbo mode)
498595- ** Matrix operations** (turbo mode)
499596- ** Numerical simulations** (turbo mode)
597+ - ** Database batch operations** (file worker turbo)
598+ - ** ETL pipelines** (file worker turbo)
599+ - ** API aggregation** (file worker turbo)
500600- Data pipelines
501601- Video/image encoding services
502602- Scientific computing
@@ -514,33 +614,47 @@ node benchmarks.js # Node
514614
515615### Results (1M items, heavy function, 12 CPUs, 10 runs avg)
516616
517- ** Bun** - Real parallel speedup:
617+ ** Bun (Windows) **
518618
519619| Mode | Time (±std) | Speedup | Main Thread |
520620| ------| -------------| ---------| -------------|
521621| main | 285±5ms | 1.00x | ❌ blocked |
522622| bee | 1138±51ms | 0.25x | ✅ free |
523- | turbo(4) | 255±7ms | 1.12x | ✅ free |
524- | turbo(8) | 180±8ms | ** 1.58x** | ✅ free |
623+ | turbo(8) | 180±8ms | 1.58x | ✅ free |
525624| ** turbo(12)** | ** 156±12ms** | ** 1.83x** | ✅ free |
526- | turbo(16) | 204±28ms | 1.40x | ✅ free |
527625
528- ** Node** - Non-blocking I/O (slower, but frees main thread):
626+ ** Bun (Linux/Docker)**
627+
628+ | Mode | Time (±std) | Speedup | Main Thread |
629+ | ------| -------------| ---------| -------------|
630+ | main | 338±8ms | 1.00x | ❌ blocked |
631+ | bee | 1882±64ms | 0.18x | ✅ free |
632+ | turbo(8) | 226±7ms | 1.50x | ✅ free |
633+ | ** turbo(12)** | ** 213±20ms** | ** 1.59x** | ✅ free |
634+
635+ ** Node (Windows)**
529636
530637| Mode | Time (±std) | Speedup | Main Thread |
531638| ------| -------------| ---------| -------------|
532639| main | 368±13ms | 1.00x | ❌ blocked |
533640| bee | 5569±203ms | 0.07x | ✅ free |
534- | turbo(4) | 1793±85ms | 0.21x | ✅ free |
535641| turbo(8) | 1052±22ms | 0.35x | ✅ free |
536642| ** turbo(12)** | ** 1017±57ms** | ** 0.36x** | ✅ free |
537- | turbo(16) | 1099±98ms | 0.34x | ✅ free |
643+
644+ ** Node (Linux/Docker)**
645+
646+ | Mode | Time (±std) | Speedup | Main Thread |
647+ | ------| -------------| ---------| -------------|
648+ | main | 522±54ms | 1.00x | ❌ blocked |
649+ | bee | 5520±163ms | 0.09x | ✅ free |
650+ | turbo(8) | 953±44ms | 0.55x | ✅ free |
651+ | ** turbo(12)** | ** 861±64ms** | ** 0.61x** | ✅ free |
538652
539653### Key Insights
540654
541- - ** Bun + turbo(cpus)** : Up to ** 1.83x faster** than main thread
655+ - ** Bun + turbo** : ** 1.6-1.8x faster** than main thread (both OS)
656+ - ** Node + Linux** : ** 0.61x** - much better than Windows (0.36x)
542657- ** bee/turbo** : Non-blocking - main thread stays ** free for HTTP/I/O**
543- - ** Node + turbo** : Slower, but useful for keeping servers responsive
544658- ** bee vs turbo** : turbo is ** 7x faster** than bee for large arrays
545659- ** Default workers** : ` cpus - 1 ` (safe for all systems)
546660
@@ -574,6 +688,7 @@ await beeThreads.turbo(data, { workers: 12 }).map(fn)
574688- ** Worker affinity** - Same function → same worker (V8 JIT)
575689- ** Request coalescing** - Deduplicates identical calls
576690- ** Turbo mode** - Parallel array processing (workers only)
691+ - ** File workers** - External files with ` require() ` + turbo mode
577692- ** Full TypeScript** - Complete type definitions
578693
579694---
0 commit comments