nodejs.org icon indicating copy to clipboard operation
nodejs.org copied to clipboard

doc: note JavaScript is not single threaded

Open benjamingr opened this issue 1 year ago • 8 comments

Honestly I appreciate the effort and don't have time to go over the whole thing but there are plenty of inaccuracies in this document.

This PR fixes the first one I noticed - JavaScript as a language is not single threaded. Nor has it ever been, and multithreaded JavaScript execution dates back at least 10-20 years

Description

Validation

Related Issues

Check List

  • [ ] I have read the Contributing Guidelines and made commit messages that follow the guideline.
  • [ ] I have run npm run format to ensure the code follows the style guide.
  • [ ] I have run npm run test to check if all tests are passing.
  • [ ] I have run npx turbo build to check if the website builds without errors.
  • [ ] I've covered new added functionality with unit tests if necessary.

benjamingr avatar Jun 19 '24 13:06 benjamingr

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nodejs-org ✅ Ready (Inspect) Visit Preview Jul 8, 2024 9:38pm

vercel[bot] avatar Jun 19 '24 13:06 vercel[bot]

If I recall this document was a port of the original guide written many years ago, so not sure who even wrote this initially.

ovflowd avatar Jun 19 '24 13:06 ovflowd

Lighthouse Results

URL Performance Accessibility Best Practices SEO Report
/en 🟢 97 🟢 100 🟢 100 🟢 91 🔗
/en/about 🟢 99 🟢 100 🟢 100 🟢 91 🔗
/en/about/previous-releases 🟢 99 🟢 100 🟢 100 🟢 92 🔗
/en/download 🟢 100 🟢 100 🟢 100 🟢 91 🔗
/en/blog 🟢 99 🟢 100 🟢 100 🟢 92 🔗

github-actions[bot] avatar Jun 19 '24 16:06 github-actions[bot]

Unit Test Coverage Report

Unit Test Report

Tests Skipped Failures Errors Time
131 0 :zzz: 0 :x: 0 :fire: 5.441s :stopwatch:

github-actions[bot] avatar Jun 19 '24 16:06 github-actions[bot]

I wonder if there's a better way to describe this. What happens in Node.js currently is:

  1. In general, each libuv event loop pairs with one JS execution thread. There are 7 threads in total by default: 1 main thread (which executes JS and runs the event loop), 4 V8 task runner threads, 1 V8 delayed task runner thread, 1 inspector I/O thread; If any async operations are initiated, 4 more threads will be spawn by the libuv thread pool, and there will be 11 threads in total. (Sometimes additional threads & event loops are spawned for e.g. watchdog for Ctrl+C signals, but those are on-demand)
  2. When we are talking about a single Node.js instance (instances = main instance or Worker thread instances), each of them has 1 JS thread which runs one event loop. Every Worker spawn adds one more thread into the process (e.g. if the application does some async operation in the main thread, spawns a worker, and then also does some async operation in the worker, there would be 12 threads in total)

To emphasize that one can spawn threads from JS, maybe it's clearer to just mention Workers. But when we are talking about event loops, they do have a 1-1 correspondance with JS threads (each worker gets their own event loop). Or we can just drop "despite the fact that JavaScript is single-threaded" completely since it's not exactly clear what this is talking about in this context.

joyeecheung avatar Jun 23 '24 16:06 joyeecheung

@benjamingr, you are correct because the language itself can not be described as single nor multi-thread because these specifications have to do with its interpreters and runners.

But I think the usage in this document could be because currently, many programmers associate the language to its interpreter.

To me, it would be better if we say that Node.js not JavaScript is single-thread.

nazarepiedady avatar Jul 05 '24 08:07 nazarepiedady

This PR will need to be rebased or recreated now that https://github.com/nodejs/nodejs.org/pull/6850 merged.

bmuenzenmeyer avatar Jul 05 '24 11:07 bmuenzenmeyer

To me, it would be better if we say that Node.js not JavaScript is single-thread.

That's also not true (e.g. https://nodejs.org/api/worker_threads.html :D ). We can add Node.js executes a single thread of user JavaScript unless other threads are explicitly created by the user or libraries, not sure that helps.

@bmuenzenmeyer I'm not sure what that means feel free to edit this branch

benjamingr avatar Jul 05 '24 18:07 benjamingr