Why is JavaScript faster than C ++?



    Yes, you heard right. Glitchy, dumb, slowed down JavaScript works faster than C ++. Wait to reach return userKarmaVote () , let me explain everything. Lawyer!

    There are three kinds of lies


    There is such a project called Benchmarks Game . The guys wrote programs (bypassing binary trees, n-body, etc.) in all popular programming languages ​​and developed a technique for measuring speed and memory consumption. Before reading further, please familiarize yourself with the measurement methodology .
    The implementation of each algorithm is described in detail (for example, nbody ). This is open-source, and if you think that some algorithm is not implemented in the most optimal way, then you can offer your own solution.

    Of all interpreted languages, JavaScipt is faster than others. It is five times faster than Python and Ruby (including JRuby).



    TodayJavaScript is the fastest interpreted language in the world .
    Returning to C ++. According to the regexdna algorithm , JavaScipt runs a little faster than C ++. At the same time, it loads the CPU half as much, although it consumes twice as much memory.
    According to other algorithms, JavaScript, of course, works more slowly than C ++, but considering that they are initially in different weight categories (compiled language with static typing versus interpreted language with dynamic), the difference is not that big.

    Why is JavaScript so fast?


    The interpreters for Python / Ruby were written by a limited number of people. These people may be insanely talented, but they had no competition at the development stage. They competed only with themselves, with their ideas about speed and quality. The interpreter for JS was born in the competition of the best minds in the world. Mozilla developed SpiderMonkey, Google developed V8, Microsoft opened Chakra. They all worked in fierce competition.

    When the NodeJS team had a question about choosing an engine for JS, they just looked at the benchmarks , saw that the V8 was much faster and chose it. If tomorrow Chakra from Microsoft will run faster than Google V8, then there will be no problem switching to it .

    Why is JavaScript so slow?


    As mentioned above, JavaScript as a language is fast. However, it is believed that the "native" purpose of JS is to manipulate the DOM. In fact, this has long been wrong and JS has been successfully used on the server, in mobile devices and even in microcontrollers. But this is not about that. The thing is that when you work with the DOM using JavaScript, it does not slow down the JS, but the DOM. There are many reasons why the DOM is so slow, but I allow myself to narrow my focus to just one reason. The problem is the HTML specification itself. Section 1.1.1 of The DOM Structure Model has the following paragraph:
    ... objects in the DOM are live; that is, changes to the underlying document structure are reflected in all relevant NodeList and NamedNodeMap objects ...

    The point is that the objects in the DOM tree are “live”. This means that whenever any part of the DOM changes, these changes are reflected in every DOM object.
    Large campaigns such as Flipboard have fought hard against DOM lags. As a result, they did not succeed and they were able to achieve 60 FPS only by replacing the DOM with Canvas. JavaScript is still here, and the lags are gone. For the same reason, Netflix abandoned the DOM on their set-top boxes, and React had to come up with its own “virtual DOM”.

    Once again: JavaScript on the client does not lag. Lays a slow DOM (more precisely, manipulations with the DOM). It doesn’t matter how you change the DOM - Java script or assembler. The DOM will still slow down. It was because of the DOM slowdown that JavaScript began to be considered a slow language. This is a historical injustice and it is high time to get rid of it.

    Webassembly


    In this regard, many have unjustified expectations from the arrival of WebAssembly. Say, WebAssembly will come in and put things in order, and we will finally abandon the “inhibited” JS. Firstly, even after the arrival of WebAssembly, work with the DOM will remain with JavaScript. Yes, some frameworks (such as AngularJS) will be able to port heavy logic to C / C ++, but the total increase from this will be minimal. Secondly, when WebAssembly can directly manipulate the DOM, bypassing JS, there will be no increase in speed, because slows down the DOM, not JS.

    Haight


    I understand that speed is not the main criterion for evaluating a language. It is necessary to take into account memory consumption, CPU load, etc. I understand that one thing is the speed of some academic algorithms, and another thing is the speed of a real production application. I understand that in addition to algorithms, there are also patterns and approaches, and that your asynchronous ASP.NET can run faster than asynchronous NodeJS.

    However, JavaScript has already been sufficiently attacked (well-deserved and undeserved) for its “strange” behavior, for its attitude to types, to inheritance, etc. But to hang on it also a label of inhibition - this is too much. Stop!

    Also popular now: