JavaScript - How much is compiled with V8? - javascript

With browsers which use the V8 JavaScript engine (JIT compilation), how much of the code is actually compiled into machine code and executed directly? Does it pick out bits or is the whole JavaScript compiled?
Also, during the execution of the compiled code what would happen if I was to assign a function to an object in JavaScript? In typical languages this would be illegal, but I thought this flexibility within JavaScript came from the fact that it was interpreted so no illegal actions were technically executed? But if its compiled what happens in that scenario?
P.S
Sorry, what I mean is what would happen is this compiled code was executed "myObject = myFunction", assuming those variables were declared elsewhere. Would this be a legal assignment?
Many thanks in advance.

Related

Does V8 compiles only the code that we want to run?

I was learning the inner workings of V8 and got a bit confused with the way it translates our code, that is, say, we have an eventListener for button and when we click that button function will execute.
Is that true that:
only eventListener and function get interpreted into bytecode?
and only the code that we want to run is interpreted into bytecode?
(V8 developer here.) Generally yes. V8 compiles code (JavaScript to bytecode) on demand, so (usually) a JavaScript function will be compiled to bytecode the first time it is run.
There may be exceptions to that statement: it's reasonable to assume that top-level code will be compiled immediately. When V8's parser sees (function, it also assumes that this is a so-called "IIFE", and compiles it eagerly based on the guess that it will end with })();. The specific details of such optimizations may change over time.
You can safely assume that when you include a large library, then all that code will not be compiled up front, but only when it is executed.

how does the jit compiler work in JAVASCRIPT

how does the jit compiler work in JAVASCRIPT ???
when we only have a 1 compilation phase which declares all of our variables and functions (during the creation of the glbal execution context ?o
...when we only have a 1 compilation phase which declares all of our variables and functions...
That's where you're going wrong. :-) Modern JavaScript engines don't have a single compilation phase. Instead, they do an initial pass through the code using something really fast, and then for code that gets reused enough to make it worthwhile, they apply an optimizer to rewrite the code in place with faster code.
In Chrome's V8, the first phase used to be a compiler (called Full-codegen) and the second phase (when needed) was an optimizing compiler called Crankshaft, but they've switched to an interpreter called Ignition for the first phase that parses the code, generates bytecode, and executes that, and then for code that makes it worthwhile, they apply an optimizing compiler called TurboFan to the bytecode. (See that blog post for the details.) This was originally to minimize the memory impact of one-off setup code, but it turned out that generating bytecode was faster than generating machine code and startup performance was actually improved as well.

A "blackbox" function in JavaScript?

Is there a way to create a "blackbox" function in a browser JavaScript environment, such that calling it does something but it's impossible from the outside world to step through what's going on inside?
To be precise, I'm looking for a way that's
Transparent algorithm-wise: The code itself is visible to anyone who sees the code.
Opaque execution-wise: While you can see the code, it's impossible for a user to step through each step in the function to inspect variables for each step.
I guess what I'm looking for is some sort of a way to create an atomic function which disallows any browser debugger based inspection, for security purposes.
As per my understanding, there is no way to hide the code from the debugger at the time of execution due to JavaScript being a scripting language, and being interpreted at runtime.
Unlike the compiled code, JavaScript's conversion to machine code is held off until the code is actually executes. That's why we defer exposing any sensitive information on the client-side.
And yes, obfuscation is possible through code minification, but still, the code can be interpreted by a user through a bit of hardwork.

How does minification work and does it affect angular nested objects?

How does minification handle $scope.obj.subObj = { key: val ...};
from what I understand the last to use variable stays unchanged,
but if I were to have html element
<div>{{obj.subObj.key}}</div>
would the result of minify shorten the code to a.b.c.key?
forgive me for asking in amateur fashion, but I'm trying to understand how javascript minification works.
From: http://en.wikipedia.org/wiki/Minification_(programming)
Minification (also minimisation or minimization), in computer programming languages and especially JavaScript, is the process of removing all unnecessary characters from source code, without changing its functionality.
So, if the minifier is able to detect that it can safely rewrite $scope.obj.subObj to a.b.c it will.
As a rule of thumb though, any variable that is from the global scope, like document, window or jQuery will not be minified because other code (outside of the scope of this file) might depend on it.
The next step up from minifying is using a compressor like Google Closure Compiler or Yahoo's YUI Compressor. These programs are typically more powerful minifiers. They can replace a function call by an in-line function for instance or change a certain method by a shorter or faster method. This requires a lot of knowledge about JavaScript and performance optimizations.
You can crank up the compression rate by dropping certain compatibility requirements but I've found the resulting code to be very unstable so I don't think we're quite there yet :)

how to force variable declaration in javascript or how to check it?

Though you can use a variable without declaring it in javascript, the misuse of variable will cause hard-to-solve errors. For example, the following code will cause endless loop.
for(i=0;i<100;i++){
document.write(fiveTimes(i));
}
...
function fiveTimes(x){
i=5;
return (i*x);
}
I'd like to know if there is a way to force every variable in the javascript to be declared before use. Or anybody knows how to check for variable declaration-before-using in tons of javascript files and blocks in a huge web server.
You can enable "Strict Mode" and you can pass your code through jsLint (Or jsHint if you have sensitive feelings :))These steps will go a long way toward making your code execute predictably.
The main architecture behind JavaScript doesn't allow you to achieve this without parsing the code with another compiler ( jsHint, etc ... ). What will happen there is that the library will double check your code for some dangerous ( unwanted ) results. If you are good enough and know what you are doing, you can avoid using those and just be careful of what you are doing.
Also, there are a lot of languages that compile to JS ( CoffeeScript, etc. ) that has automatic variable declaration for every function and more stuff you might be interested in.
This solution has to tell you that JavaScript isn't supposed to do that internally and there is no forced use of 'strict' mode at least at this time.

Categories