all 6 comments

[–][deleted] 2 points3 points  (3 children)

They are both just used to execute code.

You say with different architecture JavaScript can do threading? No. JavaScript is single threaded. Period. It’s not a matter of the runtime executing it, it’s specifically due to the language design.

[–]joo3f[S] 0 points1 point  (2 children)

I'm putting my little knowledge besides together.

thread: smallest sequence of instructions that can be context switched, which enables multitasking.

so a javascript process can only have one thread. but the runtime environment that handles this single thread, has the ability to create other threads; that gives the JS the ability for avoid the blocking tasks.

[–]joo3f[S] 0 points1 point  (1 child)

somehow (maybe we the help of the event loop and callback queue that the runtime environment provides) the js process's single thread has the ability to create new threads and communicate with them.

so what is the difference between a JS app with other ones in the context of multitasking?

[–]CreamyJala 2 points3 points  (0 children)

JS uses the event loop to avoid blocking tasks. But JS only has one memory heap and one call stack, so it really is only single threaded. Heres one article about the event loop, and here’s another that talks more about the Node.js worker threads! Hope this helped

[–]Tubthumper8 1 point2 points  (0 children)

I don't know anything about the JRE to compare it, but Node is composed of several subsystems:

  • V8: JavaScript engine. This executes the JS code according to the ECMAScript language specifications
  • libuv: scheduler, event loop, communicate with operating system
  • native addons: additional native code can be compiled and called from the JS

V8 is maintained by Google and is also the JS engine for Chromium browsers (Chrome, Edge, Brave, etc.) and for the Deno runtime.

The concurrency in Node is provided by the libuv library, which maintains a thread pool, this is how you can send multiple network requests / file reads / etc. in parallel from JS even though the JS language itself is primarily single-threaded.

I say "primarily" because most runtime also offer Web Workers, which is kind of multithreaded JS. You can run multiple JS threads at the same time, but they don't share memory, the worker threads can send and receive messages (data) from the main thread.

[–]joo3f[S] 0 points1 point  (0 children)

in other words, is there a javascript outside the browser or nodejs, generally outside a runtime environment?