US:+16362902365 & BD:+8801711573570

A Leading Web Development Company 

Sharing is caring!

Scope and the JavaScript Compiler

We’re going to treat JavaScript Engine as a human being, that we can have a conversation with, the JavaScript engine and the various parts of the JavaScript engine. And I’m going to teach you a slightly formal way of talking to that engine, and I’m going to, it’s going to feel weird, but you’re going to get some practice over this, and it’ll feel a little bit more natural over the next few minutes.

We look at these declarations and we say, okay, the JavaScript compiler is going to go through and find declarations. So the first line, the compiler is going to come through and say, ah, I see a variable declaration for an identifier called foo. Which current scope am I in? And the answer to that question is you’re in the global scope right now as we see it in this the code. Okay, I want to register the foo identifier into my current scope, which happens to be the global scope.

I move on, where’s my next declaration? Okay, my next declaration is on line three. I see a function declaration with an identifier named bar. And it’s differentiated from line one where we didn’t care about the assignment of the value. Here, we are actually going to care about the value, we’re going to care that it is actually a function that goes along with this declaration. Those really aren’t kind of separated. They’re treated as one sort of operation, at least in our concept here. So we’re going to register the function bar into the same scope. We’re currently in that scope of bar. Now it’s at this point that we need to talk about a nuance of the way JavaScript works because we’re talking conceptually about JavaScript compilation in the most basic and naive of terms. If you were a computer science student and you were told to sort of write a compiler for the JavaScript language, I took a compilers class, compiler theory class, I got to do something like that, if you were told to write a compiler, you would just sort of do this top-down approach. You just sort of scan through the code and when you recognize the declaration, you’d register it to whichever particular scope you were in. The JavaScript compilers of today are fantastically more complex than that naive computer science student implementation would be. So if you’ve ever heard of things like JIT, J-I-T, that stands for just in time compilation, the idea behind just in time compilation would say, this function bar here, we don’t see it being called. So rather than compiling the contents of the function bar, we’ll just skip over it. And we’ll come back to it later, we’ll compile the function bar whenever we are forced to because it’s been asked to be executed. So we’ll defer the compilation and we’ll compile it just in time. Even more complex than that is that many of these engines are doing, kind of started in the Firefox world where they were tracing the performance of these things, but there’s this idea that you can hot-swap the compilation of a function. So when they compile a function in JavaScript, because of all the differences in the way that types are not enforced as strongly and statically as they are in other languages, in a sense, the compiler has to kind of make a best guess as to how you’re going to use a function. It can kind of do some inferences about types and other fancy things like that, but it has to make essentially a guess as to how you’re doing this, opposed to the C++ compiler, it knows exactly how that function is going to work.

The C++ compiler has to do, it has enough time to get it right the first time, if you will. But the JavaScript engine, it doesn’t have enough time, and it doesn’t have enough information so it has to make a best guess. And so many of the engines will make a guess, but they’ll instrument that initial guess, and they’ll let the function run maybe, say, a couple of dozen times or something like that. They’ll monitor how well the guess was. Did we guess correctly? Is it being used in the way that we think it is? If the guess was incorrect, if it turns out that they didn’t do a very good job of guessing, they’ll throw away the compilation and recompile the function and hot-swap in those bits directly. So there’s all kinds of, and there’s way more even, I’m totally glossing over a bunch of (mumbles). So there’s way more to it than that. We’re going to be just the naive, top-down compiler, okay? So even though the real ones might not compile bar now, we’re going to compile bar right now, just as if we’re being a naive, top-down compiler.




Sharing is caring!

Grab the latest deals on your inbox

Your email is safe ❤️ . I hate SPAM same as you!

Techgen IT Solution provides its clients with a variety of offshore business solutions like system design, .NET technology stack including C#, ASP.NET, MVC, Web API, SQL, Entity Framework, JavaScript and related technologies, namely AngularJS, Knockout, NodeJS etc



© 2019 Techgen IT Solution. All Rights Reserved.