0

Still wrapping my head around Javascript's asynchronous nature. Help me understand this: I have several functions that need to be run on items in an array.

We have Option 1:

for (f of files){
  do_a(f);
  do_b(f);
  do_c(f);
}

vs Option 2:

for (f of files){
  do_a(f);
}

for (f of files){
  do_b(f);
}

for (f of files){
  do_c(f);
}
  1. Which would be faster?
  2. In option 1 would the do_b function fire even if do_a is still working?
  3. In option 2 would the do_b loop fire even if do_a loop is still working?
9
  • 1
    Javascript is single threaded - meaning functions cannot fire while another is still working, unless the other is interrupted or temporarily releases control. If the functions that operate on the elements do not interfere with one another, option 1 is better. Especially on files, where cache misses could play a considerable role. Commented Aug 12, 2017 at 15:52
  • 1. I'd say option 1 since you're only looping once. 2. yes, if do_a is asynchronous. 3. yes, if do_a is asynchronous. Commented Aug 12, 2017 at 15:52
  • Unless the functions themselves are asynchronous, this question isn't about JavaScript's asynchronous nature, just performance. Commented Aug 12, 2017 at 15:54
  • 1
    And what exactly is asynchronous here? Commented Aug 12, 2017 at 15:56
  • Based on the comments I'm gathering that Javascript is synchronous unless explicitly specified as asynchronous (ie, in how the function is defined)? Commented Aug 12, 2017 at 15:59

2 Answers 2

1
  1. It is not clear

  2. Yes, the function will be called

  3. No, the first loop would complete before second loop begins

Sign up to request clarification or add additional context in comments.

4 Comments

Just to clarify on #3: the loop would be completed, but the do_a functions in it might still be running if they are asynchronous
By "it's not clear" I assume you mean that's dependent on the nature of the functions... But all things being equal (lets say each function takes exactly T time to finish) would 3 loops each taking Tx1 time be faster than 1 loop taking Tx3 time?
@blex That is a matter of how the pattern is constructed. Even if the call is asynchronous, the next loop can be composed to wait until the first loop completes
@Trees4theForest "It is not clear" is based on the facts at Question. "faster" is left undefined. No current benchmarks appear at Question for the approaches. It would be speculation to state which approach would be "faster" - run the tests to get the accurate results
1
  1. Which would be faster?

It's hard to say.

  1. In option 1 would the do_b function fire even if do_a is still working?

While Javascript supports asynchronous programming the code you have presented here is completely synchronous and so the functions will do work in the order you have written them.

  1. In option 2 would the do_b loop fire even if do_a loop is still working?

No, for the same reason.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.