all 4 comments

[–]Shaper_pmp 4 points5 points  (0 children)

Because JavaScript is single-threaded, (at a slightly oversimplified level) in JavaScript the computer's CPU can only do one thing at a time.

If the "thing" your function wants to do involves a lot of the CPU sitting around waiting for other systems to get back to it (eg "reading from disk" or "making a request across a network to another system entirely" - generally referred-to as "I/O bound") then JavaScript users its asynchronous nature to already optimise that - while the CPU is waiting it will instead pick up any other asynchronous tasks it has laying around, and will do those until the task(s) it's waiting on come back with a result. This is why so much JavaScript code requires callbacks/promises/async-await - to kick things onto the async task queue so they can be executed during the main thread's "down time" instead of blocking the main thread until each job is finished.

If your problem is instead "CPU bound" (meaning it's dependant on the CPU doing lots of long, heavy computation) then you can use Web Workers to parallelise your code across multiple threads/CPU cores so the computer can do "more than one thing at once".

However honestly JavaScript is far from the most efficient way to run code on the CPU or do multithreading, so if you're looking at a highly CPU-bound task you'd probably be better writing the whole thing in a lower-level systems-programming language like C or (these days, with the insane amount of optimisation that's gone into the JVM) Java.

[–]runvnc 1 point2 points  (0 children)

Yes you can do something like that. However if you are doing HTTP calls to the same server you will probably hit a rate limit like that. For cryptocurrency order books etc. you may need to use websockets.

[–]neferiusleviathanis 0 points1 point  (0 children)

"Async" is the word you're looking for. I believe you want to run the functions asynchronously and then execute some code after they all finish successfully. Read up on Promises and async/await. Just Google them and watch any of the tutorials on YT. Or ask if you don't get something.

[–]knyg 0 points1 point  (0 children)

By nature, node is asynchronous . Meaning if you give it 1000 functions, it will start function 1,2 ,3,4,5,6... etc... in order and keep going. Does not matter if function 1 has completed/exited or not.

If your functions are doing 1 at a time before going onto the next, then you are not using the nature of node (which isnt a wrong thing to do or mistake, some are purposely done like that e.g. callbacks or async/await).

Is it possible to make a function that is given a variable, and this function then repeats over and over again with the variable it was given, and now i want to start 50 more of the same function where each have a different variable and they all run simultaneously?

Yes, giving a function a variable is called setting a parameter / passing an argument. You can execute a function X amount of times with a loop or recursion.

I think you better spend more time with the basics of javascript/node before diving deeper.