nodejs / nodejs.org

The Node.js® Website
https://nodejs.org
MIT License
5.99k stars 6.15k forks source link

doc: note JavaScript is not single threaded #6848

Open benjamingr opened 1 week ago

benjamingr commented 1 week ago

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

vercel[bot] commented 1 week ago

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

Name Status Preview Updated (UTC)
nodejs-org ✅ Ready (Inspect) Visit Preview Jun 19, 2024 1:17pm
ovflowd commented 1 week ago

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

github-actions[bot] commented 1 week ago
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] commented 1 week ago

Unit Test Coverage Report

Lines Statements Branches Functions
Coverage: 92%
90.67% (593/654) 76.08% (175/230) 94.57% (122/129)

Unit Test Report

Tests Skipped Failures Errors Time
131 0 :zzz: 0 :x: 0 :fire: 5.388s :stopwatch:
joyeecheung commented 1 week ago

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.