How Does Javascript work?

sivakumar raju
Nov 5 · 4 min read
Javascript Mechanism
Javascript Mechanism

JavaScript is what is called a Client-side Scripting Language. … When the browser loads the page, the browser has a built-in interpreter that reads the JavaScript code it finds on the page and runs it.

Sample Javascript program
Sample Javascript program

The above-Shown program represents printing ‘hello world’ in the console using javascript Engine.

What is the Purpose of Using Javascript at Client-side?

— ->Reduce Load-Balance on Client-Server. Consider your logging into a website using E-mail id & password it means that you are sending a request to a server and check Database to validate the user without javascript we need to send request Every time to the server to check validation.

— ->Using Javascript we can validate the Authentication details at clientside and request the server once to check the database for validation. This reduces the server load. we can validate the right user using a captcha which restricts the intruders.

Javascript Engine:

— ->“V8 is Google’s open-source high-performance JavaScript and WebAssembly engine, written in C++” (V8 documentation) but what does this actually mean? Well, actually it means, V8 is a C++ program, which receives JavaScript code, compiles, and executes it.

V8 Does:

  1. Compiles and executes JS code
  2. Handling call stack — running your JS functions in some order
  3. Managing memory allocation for objects — the memory heap
  4. Garbage collection — of objects which are no longer in use
  5. Provide all the data types, operators, objects and functions

V8 Can:

  1. Provide the event loop, but this is sometimes implemented by the browser as well

V8 Doesn’t:

  1. Know anything about the Document Object Model (DOM) — which is provided by the browser, and obviously irrelevant to Node.js for example

V8 is a single-threaded execution engine. It’s built to run exactly one thread per JavaScript execution context. You can actually run two V8 engines in the same process — e.g. web-workers, but they won’t share any variables or context like real threads. This doesn’t mean V8 is running on a single thread, but it does mean it provides a JavaScript flow of a single thread.

On the runtime, V8 is mainly managing the heap memory allocation and the single-threaded call stack. The call stack is mainly a list of functions to execute, by calling order. Every function which calls another function will be inserted one after the other directly, and callbacks will be sent to the end. This is actually why calling a function with setTimeout of zero milliseconds sends it to the end of the current line and doesn’t call it straight away (0 milliseconds).

Other Key Components:

JS Interpreter — Ignition & Optimization Compiler — TurboFan & Crankshaft

V8 gets its speed from just-in-time (JIT) compilation of JavaScript to native machine code, just before executing it. First of all, the code is compiled by a baseline compiler, which quickly generates a non-optimized machine code. On runtime, the compiled code is analyzed and can be re-compiled for optimal performance. Ignition provides the first while TruboFan & Crankshaft the second.

JIT compilation result machine code can take a large amount of memory, while it might be executed once. This is solved by the Ignition, which is executing code with less memory overhead.

The TurboFan project started in 2013 to improve the weakness of Crankshaft which isn’t optimized for some part of the JavaScript functionality e.g. error handling. It was designed for optimizing both existing and future planned features at the time.

Conclusion

JavaScript is not aiming to be the most optimized server-side language for high scale and throughput. Nevertheless, since the introduction of V8 and the above architectural improvements, The set of tools a web developer can use transformed completely, enabling huge improvements and new features.

I hope this quick taste of the V8 helped you gain some simplified basic understanding of the V8 engine which is running the JavaScript code on the client & server.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade