Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does this for loop iterate multiple times?

I have been discussing some code with colleagues:

for(const a of arr) {
  if(a.thing)
    continue;
  // do a thing
}

A suggestion was to filter this and use a forEach

arr.filter(a => !a.thing)
  .forEach(a => /* do a thing */);

There was a discussion about iterating more than necessary. I've looked this up, and I can't find anything. I also tried to figure out how to view the optimized output, but I don't know how to do that either.

I would expect that the filter and forEach turn into code that is very much like the for of with the continue, but I don't know how to be sure.

How can I find out? The only thing I've tried so far is google.

like image 763
loctrice Avatar asked Dec 31 '18 19:12

loctrice


People also ask

Can we use for loop two times?

In the for loop, this is happening because of the initialization part i.e., for(i=0;..). Different kinds of loops provide convenience to a programmer while writing code and one can use any kind of loop any number of times in a program.

Does for loop check condition every time?

Here, initialization sets the loop control variable to an initial value. Condition is an expression that is tested each time the loop repeats. As long as condition is true, the loop keeps running.

How many iterations in while loop?

The “while” loop While the condition is truthy, the code from the loop body is executed. A single execution of the loop body is called an iteration. The loop in the example above makes three iterations. If i++ was missing from the example above, the loop would repeat (in theory) forever.


1 Answers

Your first example (the for in loop) is O(n), which will execute n times (n being the size of the array).

Your second example (the filter forEach) is O(n+m), which will execute n times in the filter (n being the size of the array), and then m times (m being the size of the resulting array after the filter takes place).

As such, the first example is faster. However, in this type of example without an exceedingly large sample set the difference is probably measured in microseconds or nanoseconds.

With regards to compilation optimization, that is essentially all memory access optimization. The major interpreters and engines will all analyze issues in code relating to function, variable, and property access such as how often and what the shape of the access graph looks like; and then, with all of that information, optimize their hidden structure to be more efficient for access. Essentially no optimization so far as loop replacement or process analysis is done on the code as it for the most part is optimized while it is running (if a specific part of code does start taking an excessively long time, it may have its code optimized).

When first executing the JavaScript code, V8 leverages full-codegen which directly translates the parsed JavaScript into machine code without any transformation. This allows it to start executing machine code very fast. Note that V8 does not use intermediate bytecode representation this way removing the need for an interpreter.

When your code has run for some time, the profiler thread has gathered enough data to tell which method should be optimized.

Next, Crankshaft optimizations begin in another thread. It translates the JavaScript abstract syntax tree to a high-level static single-assignment (SSA) representation called Hydrogen and tries to optimize that Hydrogen graph. Most optimizations are done at this level.
-https://blog.sessionstack.com/how-javascript-works-inside-the-v8-engine-5-tips-on-how-to-write-optimized-code-ac089e62b12e

*While continue may cause the execution to go to the next iteration, it still counts as an iteration of the loop.

like image 172
Travis J Avatar answered Oct 12 '22 14:10

Travis J