r/javascript • • Dec 05 '25

The missing standard library for multithreading in JavaScript

https://github.com/W4G1/multithreading
146 Upvotes

33 comments sorted by

View all comments

Show parent comment

3

u/Waltex Dec 06 '25 edited Dec 06 '25

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.

1

u/Fidodo Dec 06 '25

That's what I was looking into too. As you say the downside is that you lose a lot of scope safety. It should be possible to add that with a linter rule, I'd really love that feature! I think it would be unnecessary overkill to add any runtime checks.

Looks really promising! I'm going to look into it.