News for hackers. Stuff that matters.
Re:Windows 3.1 All Over Again by chmod775 on Sunday September 24, 2017 @10:16PM (#15327645)
Can we stop repeating the meme that Node.JS is not multi-threaded?

True, your JS code all runs in a single thread, but all the heavy lifting is behind asynchronous interfaces, delegating the work to a threadpool in the background.

What a Node.JS application really is, is a supervisor thread (your JS code) giving work for a bunch of worker threads. Doesn't sound very single threaded to me.

Re:Windows 3.1 All Over Again by Mithaldu on Sunday September 24, 2017 @11:10PM (#15327813)
Can you start a thread with a JS loop in node so you can have two loops running at the same time doing CPU-heavy tasks?

If not, then calling it multi-threaded is misleading to a programmer who needs such things, and instead you'll need to find a more accurate word.

Re:Windows 3.1 All Over Again by chmod775 on Monday September 25, 2017 @09:04AM (#15330060)
This will repeatedly run 100000 iterations of PKBDF2 in 10 threads:

    process.env.UV_THREADPOOL_SIZE = 10;

    function startOne() {
        const start = Date.now()
        require('crypto').pbkdf2(String(start), 'secretSalt', 100000, 512, 'sha512', (error, result) => {
            console.log(`${result.length}b took ${Date.now() - start}ms`);
            startOne();
        });
    }

   for (let i = 0; i < 10; i++) startOne();
You can use htop or another tool of your choice to confirm this is the case.
Re:Windows 3.1 All Over Again by Mithaldu on Tuesday September 26, 2017 @02:41AM (#15336667)
Are the threads started by way of external process, thread started within a C library, or started with an actual Node.js call i can write directly in node.js code and also use to run literally arbitrary node.js code?
Re:Windows 3.1 All Over Again by chmod775 on Wednesday September 27, 2017 @11:35AM (#15348835)
If I'm going to assume that by "node.js" code, you actually mean JavaScript code running within the V8 engine as a part of the whole that is Node.JS, then: The answer is no, you cannot do that using pure JS and without using (external) stuff like WebWorkers.

If you are talking more generally about anything we can run within Node.JS, is using Node.JS user-facing APIs and works without doing any modifications to Node.JS, the answer is yes. You can easily achieve that with node's native module support, and I would encourage you to do so over committing the folly of doing anything CPU intensive in JS.

That is if you manage to find any CPU intensive task that isn't already handled by node built-ins or some library out there.

Edit:

Now you may say: "Ahah! You can't use threads using [limited usage of node]. Also threads must be usable for arbitrary workloads in order to consider a thing multi-threaded. Node.js is single-threaded!"

I just can't argue with that. It's just a matter of opinions now about what makes a thing/environment single-threaded, and what makes it multi-threaded.

To me it's simple: [x] Thing uses multiple threads to perform work.

Re:Windows 3.1 All Over Again by romanovcode on Monday September 25, 2017 @04:55AM (#15328893)
> delegating the work to a threadpool in the background.

Yes, delegating to the C++ modules in the background, which do support multi-threading.

These modules have nothing to do with Node directly so meme stays - node itself is single-threaded no matter how you spin this off.

Re:Windows 3.1 All Over Again by chmod775 on Monday September 25, 2017 @08:49AM (#15329959)
> delegating to the C++ modules in the background

Eh, what are these if not part of node?

What kind of definition are you applying so that a thing is suddenly no longer the sum of its parts?

Edit: JavaScript may be single threaded. Node.JS is not. JavaScript is but a part of Node.JS.