For example:
var longArray = [1, 2, 3, 4]
var shortArray = [2, 3]
Which one is faster?
Long loop first:
for (var i = 0; i < longArray.length; i++) {
for (var j = 0; j < shortArray.length; j++) {
if (longArray[i] === shortArray[j]) {
// do something
}
}
}
Or short loop first:
for (var i = 0; i < shortArray.length; i++) {
for (var j = 0; j < longArray.length; j++) {
if (longArray[i] === shortArray[j]) {
// do something
}
}
}
Or are there any advantages in either that I'm not considering? Or does it not matter at all?
NESTED LOOPS joins have an advantage over other join methods in that they can quickly retrieve the first few rows of the result set without having to wait for the entire result set to be determined.
Most likely neither are measurably faster and they may get optimized to the same exact code depending on the compiler.
In a nested loop, a break statement only stops the loop it is placed in. Therefore, if a break is placed in the inner loop, the outer loop still continues. However, if the break is placed in the outer loop, all of the looping stops.
The loop which contains a loop inside a loop is known as the nested loop. It can contain the for loop inside a for loop or a while loop inside a while loop. It is also possible that a while loop can contain the for loop and vice-versa.
When a loop is nested inside another loop, the inner loop runs many times inside the outer loop. In each iteration of the outer loop, the inner loop will be re-started.
4.4. Nested For Loops — AP CSAwesome 4.4. Nested For Loops ¶ A nested loop has one loop inside of another. These are typically used for working with two dimensions such as printing stars in rows and columns as shown below. When a loop is nested inside another loop, the inner loop runs many times inside the outer loop.
If a loop exists inside the body of another loop, it's called a nested loop. Here's an example of the nested for loop. Here, a for loop is inside the body another for loop. It should be noted that you can put one type of loop inside the body of another type. For example, you can put a while loop inside the body of a for loop.
In many cases we may use loop statement inside another looping statement. This type of looping is called nested loop. In nested loop the inner loop is executed first and then outer. The nested loop must be used to input or output multi-dimensional array elements..
Short loop first is generally faster, only because it spends more time visiting elements in the same order as they tend to be laid out in memory. You need longArray
to have a lot more elements, at least several thousand. Here is a test case to demonstrate the difference: https://jsperf.com/loop-order-sl
For the arrays you posted, which has a longArray
that is relatively small, there is negligible difference in performance. The shorter array might be a tiny bit faster as @Ajaypayne observed or vice-versa depending on environment.
There is no significant difference between the two. Depending on how you benchmark it you might get a slight speed increase one way or another but it will never amount to much. In the end you will have i*j
total iterations. So, how do you make a decision on which way to go?
Depending on the specific circumstances you may be able to eliminate certain iterations. @Mark_M hits on this in his comment about the sorting. If you are checking for equality and they are both sorted then you can exit the inner loop as soon as you get a hit. Little things like this can add up to a significant overall gain in particularly long running code. (Not that I would ever run a linear search through sorted arrays but that isn't the point here.)
If there is clear way to shorted the looping as described in (1), then shoot for clarity rather than performance. Even if you are only coding for yourself, looking at code you wrote 6-months ago can often be bewildering. Choose which seems more natural or obvious and save yourself (and others) headaches later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With