Giter VIP home page Giter VIP logo

how-javascript-works's Introduction

How javascript works ?

Javascript Engine

  • Javascript integrate in browsers the Javascript Engine. It is composed of two things :
    • The memory heap (allocate memory).
    • The call stack (read and execute code).

Engine

Memory Heap

  • Allocate memory. Example : const name = 'Antonin'.
  • When there are too many global variables to save large objects, we can talk about Memory Leak (overflowing) and if some are not used.

Call Stack

  • Execution of an operation (one by one). Example : console.log('toto').

My code :

	console.log(1);
	console.log(2);
	console.log(3);

In call stack (one by one execution on the call stack, synchrone execution) :

|--------------|
|console.log(1)| first execution
|console.log(2)| second execution
|console.log(3)| last execution
|--------------|
  • Javascript is a single-thread language, it works with only one call stack. It's different from multi-thread.
  • When there are too many operations to execute, it's stack overflow.

Single-thread non-blocking ?

  • The synchronous is blocking (we have to wait until the previous task is finished).
  • It is necessary to use asynchronous to have single-thread non-blocking (Run-Time Environment on Javascript).

Loop system

Engine

  • 1- Check if there are operations in the call stack.
  • 2- If empty, check if there are operations in the callback queue.
  • 3- If there is an operation, it is transferred to call stack and executed.

Example

Code :

console.log('start') // In call stack
element.addEventListener('click', event => event.target.remove()) // In 'callback queue' and transferred in 'stack call' if element are clicked
console.log('end') // In call stack

Summary

  • Javascript is an single-thread language and can be non-blocking with Asynchronous operations.

how-javascript-works's People

Contributors

antonintouron avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.