GithubHelp home page GithubHelp logo

javascript-engine's Introduction

JAVASCRIPT ENGINE

Javascript Engine

  • reads JS code
  • converts it to machine code (binaries)
  • ie V8 by Google (written with C++)

What is Inside JS Engine

  • the bigges thing is reading our code and executing it
  1. MEMORY HEAP - we need a place to store and write information (ie variables, ...)
    - all programs are just read and write operations
    - memory allocation
    - watch out for MEMORY LEAKS (when garbage collection fails)
  2. CALL STACK - a place to run our code in order
    - keeps track of where we're at in our code in execution
    - watch out for STACK OVERFLOW

Single Threaded

  • only one instruction at a time - 1 call stack
  • no instructions in parallel
  • what if we have an instruction that takes a while to resolve

JS Runtime

What is Inside JS Engine II

  1. PARSER - lexical analysis; breaks the code into tokens
  2. AST - abstract syntax tree
  3. INTERPRETER - JS is mostly interpreted
    - translates and reads the files line by line
    - no conversion to lower language first; goes straight to running the code line by line
    - perfect for JS on the fact that the program has to be user friendly on the web
    - it will get slower and slower though; ie it always has to iterate a loop whereas compiler will optimize this
  4. COMPILER - goes through the source code once to understand and optimize it (by compiling it to another programming language)
    - takes a little bit more time to start up because of compiling but optimizes code, ie replacing a function with just its return
  5. JIT COMPILER - combination of both
    - the INTERPRETER part will run the code line by line ( bytecode)
    - a PROFILER watches the INTERPRETER and does optimization on it simulataneously (passes the problematic code to COMPILER)
    - COMPILER replaces parts of the bytecode => OPTIMIZED CODE

Optimized Code

  1. Inline Caching - replacing codes with just the 'results' of it, so any instance of it will be optimized
  2. Hidden Classes - instantiate your classes in order
function Animal(x,y) {
  this.x = x
  this.y = y
}

const obj1 = new Animal(1,2)
const obj2 = new Animal(3,4)

obj1.a = 30       // compiler will be slower here because of orders
obj1.b = 100
obj2.b = 30
obj2.a = 100
  • write codes that are predictable to humans and to the computer

ECMAscript Engine

  • standard in JS

javascript-engine's People

Contributors

kylenrich24 avatar

Watchers

James Cloos avatar  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.