Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2007
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How does a browser run JavaScript?

    Hi Everyone,

    I have a technical question regarding how JavaScript is run by browsers.

    In traditional programming a source file (containing human readable programming instructions) is first compiled (into binary data) before it can be run by a computer.

    Do JavaScript commands get compiled before being run, or does the "scripting engine" interpret the human readable commands directly?

    Any help here will be greatly appreciated.

    Kind Regards,

    Davo

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,591
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    In theory, the implementation of JavaScript is left as an exercise to the creator of each JavaScript engine.

    In practice, all browsers *do* implement JavaScript using a compiler. But most compilers in use don't compile the source code into machine code directly. Rather, they compile into an intermediate form (sometimes called a "byte code") and then that intermediate code is interpreted.

    BUT...

    But there is an elephant in the room: Google's V8 JavaScript compiler *IS* a true compiler, compiling directly to machine code.

    So the answer to your question is the usual answer to almost all programming questions: "It depends ..."

    Oh, and if you are curious:
    https://developers.google.com/v8/?csw=1
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Sep 2007
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you for the insight

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,296
    Thanks
    13
    Thanked 345 Times in 341 Posts
    another good explanation, esp. with regards to browsers: http://www.html5rocks.com/en/tutoria...browserswork/?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,350
    Thanks
    11
    Thanked 589 Times in 570 Posts
    browsers past and current all interpret js code as its parsed. All are capable of interpretation, they have to be. all js code is interpreted, at least at first.

    Many newer engines (odin,chakra,v8) also compile a low-level code version that is fairly comparable to a compiler's output, which they switch into once all the code is loaded and parsed.

    For V8 this is a 3-level stage: user-land js, low-level strong-typed+optimized interpretation, and ultimately compiled machine code.

    M$'s chakra engine leaves interpreted mode upon completion of a background CPU core compiling the javascript code into jscript.net (more or less) IRL or bytecode.

    Tracemonkey is a highly-advanced and performant interpreter used in mid-firefox versions. Though the epitome of interpretation (it can outperform naive compilers), mozilla has integrated JIT features to it over the years, especially during Ion, which is why firefox now enjoys the fastest JS engine in production.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •