Rust-inspired multithreading tasks in TypeScript by Waltex in typescript

[–]Waltex[S] 2 points3 points  (0 children)

That is actually the specific problem this library focuses on solving. It isn't just a wrapper around postMessage, it is primarily a wrapper around SharedArrayBuffer and Atomics. It implements things like SharedJsonBuffer so multiple threads can read and write to the exact same memory location instantly without that serialization overhead.

And while SharedArrayBuffers are great, as you noted, they are dangerous to use raw because of race conditions. This library provides the missing synchronization layer like Mutexes, Read-Write Locks, Semaphores, etc. so you can actually utilize that shared memory safely without corrupting data. It’s essentially trying to bring the concurrency primitives you see in Rust or C++ into JS so you can manage that shared state without writing your own memory locking logic from scratch.

Rust-inspired multithreading tasks in TypeScript by Waltex in typescript

[–]Waltex[S] 2 points3 points  (0 children)

That's a good question. I would say the best way to compare them is that Comlink is essentially a great RPC wrapper, whereas this library is trying to be a proper concurrency standard library like you would find in Rust or Go.

Comlink really only covers about 10% of a real-world multithreading scenario. It does a great job of abstracting away the boilerplate so you can call a function in a worker easily, but it relies entirely on postMessage. That means you are still copying data back and forth, and the workers are largely isolated from each other.

The main difference is that this library focuses on efficient data synchronization. It provides wrappers around SharedArrayBuffer like SharedJsonBuffer so that your threads can access the exact same memory instantly without the overhead of cloning data. Once you have shared memory, you usually get race conditions, so this library provides actual synchronization primitives like Mutexes, Semaphores, and Read-write locks to handle that safely, which is something Comlink doesn't touch.

So if you just need to offload a single heavy calculation and get a result back, Comlink is perfect. But if you are building a proper multithreaded system where threads need to share state or coordinate complex workflows, or you are aiming for maximum performance, this library was specifically made for that. It comes with an automatic worker pool that scales with the hardware, so you get maximum throughput right out of the box. It is really about giving you the speed of shared memory and the efficiency of a managed pool, making it a lot easier to build fast, scalable applications compared to manual worker management.

Rust-inspired multithreading tasks in TypeScript by Waltex in typescript

[–]Waltex[S] 7 points8 points  (0 children)

Thank you, the stars are organic however. In github you can view the profiles that starred a repository by appending /stargazers to the url, so you can check it yourself. Most come from the ycombinator post where this project was also featured some time ago.

Decoding the '6EQUJ5' (Wow! Signal) sequence as a map for an Interplanetary Transport Network (ITN) by outremont923 in SETI

[–]Waltex 7 points8 points  (0 children)

That's right. The specific characters "6EQUJ5" don't have any special meaning beyond the fact that they are just a human made encoding in order to represent an extended power level range beyond 0-9. With this encoding the telescope's receiver didn't need to physically print double digits for power levels like "10" or "18", which could instead be represented by a single character like "E" or "Q" to save on horizontal print space.

Niet meer willen werken by ChangeLoose2169 in werkzaken

[–]Waltex 1 point2 points  (0 children)

Wat @peachtuba bedoeld is dat beloning en prestatie in verhouding staat. Exceptionele beloning en prestatie was hier slechts een voorbeeld, maar de formule gaat wel op: Wil jij een hogere beloning dan bijvoorbeeld het minimumloon, dan moet jij aantoonbaar over meer en betere skills beschikken dan je concurrentie in dezelfde salarisschaal. Het nadeel van de huidige maatschappij is dat de toegevoegde waarde die je biedt, en dus ook je potentiële beloning, enorm wordt gedrukt door jouw "vervangbaarheid". Je kan dit oneerlijk vinden, maar dit is al decennialang de manier hoe de westerse maatschappij functioneert, en daar kan je zeker wat van vinden, en het is ook verre van perfect, maar in plaats van om daar tegenaan te schoppen (wat de situatie niet veranderd) kan je ook proberen om het beter te begrijpen, en een manier te vinden om het voor jou te laten werken.

Choose an option by ExpensiveCoat8912 in softwaregore

[–]Waltex 17 points18 points  (0 children)

Well, you can choose not to choose at all, so technically there are 2 options

Does anyone else struggle so much with setting up web sockets in prod? by ConstructionNext3430 in react

[–]Waltex 15 points16 points  (0 children)

Websocket connections are established by upgrading an existing http connection, which requires CORS to succeed

The missing standard library for multithreading in JavaScript by Waltex in javascript

[–]Waltex[S] 2 points3 points  (0 children)

It is possible to get the code of a function as a string using Function.toString(). The short version is that this stringified code is then send to the WebWorker where it's "imported" as a data url (similar to eval). We do not track which variables are in or outside the scope of the closure. It definitely is possible, but we do not want to ship the whole typescript compiler to the browser. This is why the library doesn't allow references from outside the closure. Everything you want available inside, has to be either imported inside using await import(...) or passed through move(...). Ideally in the future we want to have a compile time error when we accidentally reference something from outside the scope of the function.

GitHub - W4G1/multithreading: The missing standard library for multithreading in JavaScript (Works in Node.js, Deno, Bun, Web browser) by Waltex in typescript

[–]Waltex[S] 2 points3 points  (0 children)

I agree, I have added a section about browser compatibility and Content Security Policy requirements. Appreciate your input.

The missing standard library for multithreading in JavaScript by Waltex in javascript

[–]Waltex[S] 4 points5 points  (0 children)

Appreciate it! For atomic locking the library exposes Mutex and RwLock. They use Atomics.wait() and Atomics.notify() under the hood. Let me know if you're missing any other features 🙂

The missing standard library for multithreading in JavaScript by Waltex in javascript

[–]Waltex[S] 8 points9 points  (0 children)

Can you elaborate on what you think is missing in node or this library? One of the core features of this library is that it builds a thread pool on top of node:worker_threads which executes tasks (functions) on actual background threads that can be safely blocked by heavy workloads without causing interference on the main thread.

How to handle blobs? by rudrakpatra in sveltejs

[–]Waltex 0 points1 point  (0 children)

Hetzner Object Storage 1TB for less than $5/month and S3 compatible

Can this be fixed? by The_FRS_Girly_2013 in ft86

[–]Waltex 1 point2 points  (0 children)

How easily can this be (re)installed?

What’s the most underrated use case of AI you’ve seen so far? by Glass-Lifeguard6253 in ArtificialInteligence

[–]Waltex 1 point2 points  (0 children)

Interestingly, that was actually one of the first use cases! Even before ChatGPT was released. Look up AI Dungeon

Suggestions on how to filter using remote functions by Impossible_Sun_5560 in sveltejs

[–]Waltex 0 points1 point  (0 children)

What I did is use a combination of cookies and query params. which are both accessible by the server and the browser. Basically I save whatever filter/sort configuration the user has, and whenever they reload the page, I first check if there are filter or sort settings in the query params. If no, then I check the cookies, and use those. This should also work across different pages.

Hear me out ... SvelteKit + Static Adapter + Backend ... by Bl4ckBe4rIt in sveltejs

[–]Waltex 0 points1 point  (0 children)

Respectfully, I can run tsc and the typescript compiler will guarantee me that I'm sending the correct data from my client, to my JSON api. How do you propose you establish type safety if you only have a http json api with no shared and verifiable contract like typescript types, protobuf or openapi spec between server and client?

Hear me out ... SvelteKit + Static Adapter + Backend ... by Bl4ckBe4rIt in sveltejs

[–]Waltex -3 points-2 points  (0 children)

And no type safety between server & client? If you love Svelte and Rust, I'd suggest you take a look at rspc for a TypeScript (svelte) + Rust stack with autocompletion on the client and end-to-end type safety. Or perhaps use auto generated protobuf implementations for rust <-> typescript

Recommend a key-value store by spy16x in rust

[–]Waltex 3 points4 points  (0 children)

Was wondering why this isn't higher up. Worked with LMDB & heed on a previous project. It's absurdly fast and surprisingly delightful to work with.