3

Today I've got an idea to check performance of loop which I have called "scoped for". The idea is simply. This loop has two variables, "i" and "l" which are defined "one scope higher" than loop itself. There's nothing else in those two scopes.

I've created jsPerf and got amazing results. http://jsperf.com/variable-scoped-loop/6

I decided to create my local test, and results are even better ( 1000x1000 loops average time of 5s for "standard for" and under 0.01s for "scoped for" ).

So now I am wondering why this loop is so damn fast. I`m assuming that it's all about V8, but you never know.

So anyone willing to explain?

TLDR :

Why this loop is so damn fast?

var loop = ( function() {
                var i, l;

                return function( length, action ) {
                    for( i = 0, l = length ; i < l ; ++i ) {
                        action();
                    }
                };
            }() );
7
  • I guess engine has to create variables i and l at each loop for the first two cases, while he point to the same i and l at each loop for the two last cases. Don't know if this makes that much difference tho. Commented Sep 21, 2015 at 11:49
  • 10
    Could the javascript engine notice that you aren't using the results of the loop and optimize the whole loop away? Commented Sep 21, 2015 at 11:50
  • 3
    It seems you have outsourced data crucial for the understanding of your question. Please post all relevant code, images and resources in the question itself, if the link dies or changes your question will lose most if not all of its meaning! Commented Sep 21, 2015 at 11:51
  • @Philipp The perfs are really bad for the two first cases (var in for & var in fun), maybe the question was more about, why loop using closure are way faster than "standard" ones? Commented Sep 21, 2015 at 11:55
  • @Philipp Basically I wanted to see the performance differences between every approach, even those bad ones. And thats the point of a question exactly - why "scoped" loop is so damn faster than "standard" one. I think that it's all about creating new "i" and "l" variables on each loop. And maybe also GC is doing some work too. Commented Sep 21, 2015 at 12:05

1 Answer 1

2

Unfortunately, there's no magic here: your test is faulty.

For varInFor, the empty function is correctly called 9999^2 times, whereas with varInScope, it's only called 9999 times. That's why it finishes a lot quicker. You can test this easily by making the empty function print something.

The reason why is the fact that variables i and l are shared between the outer and inner call of varInScope. So after the inner loop finishes, i is already equal l and the outer loop immediately exits.

See another JSPerf for a fixed version that initializes the functions every time (to create a new set of variables in the closure) and it is, indeed, up to 20% slower than the "normal" for loop.

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

1 Comment

Thanks for pointing this out! To be honest, I`m ashamed that I've missed such a thing... Anyway the "mystery" has been revealed :)

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.