Slashdot Mirror


Firefox Gets Massive JavaScript Performance Boost

monkeymonkey writes "Mozilla has integrated tracing optimization into SpiderMonkey, the JavaScript interpreter in Firefox. This improvement has boosted JavaScript performance by a factor of 20 to 40 in certain contexts. Ars Technica interviewed Mozilla CTO Brendan Eich (the original creator of JavaScript) and Mozilla's vice president of engineering, Mike Shaver. They say that tracing optimization will 'take JavaScript performance into the next tier' and 'get people thinking about JavaScript as a more general-purpose language.' The eventual goal is to make JavaScript run as fast as C code. Ars reports: 'Mozilla is leveraging an impressive new optimization technique to bring a big performance boost to the Firefox JavaScript engine. ...They aim to improve execution speed so that it is comparable to that of native code. This will redefine the boundaries of client-side performance and enable the development of a whole new generation of more computationally-intensive web applications.' Mozilla has also published a video that demonstrates the performance difference." An anonymous reader contributes links the blogs of Eich and Shaver, where they have some further benchmarks.

31 of 462 comments (clear)

  1. Re:As fast as C code??? by Anonymous Coward · · Score: 5, Informative

    Correct me if I'm wrong, but generally speaking, I was always under the impression that, as an interpreted language, javascript will never be able to run 100% as fast as natively compiled C code.

    Well, it has a JIT compiler, so that would transform it into bytecode essentially. It's the reason why C# is as fast as C code, because it too has a JIT. Google JIT :)

  2. Re:As fast as C code??? by Bjarke+Roune · · Score: 5, Informative

    The optimization in the story is to compile parts of code written in Javascript. So when using this optimization, the Javascript is only partly interpreted, and if the compiled part is the part that takes up most of the runtime, then the Javascript could conceivably be something like the speed of natively compiled C.

  3. Re:Precursor to more of Firefox being in JS by i.of.the.storm · · Score: 2, Informative

    Well, XUL is just an XML language used for the UI. As far as I know the main codebase is C++/JS right now, but probably more C++ than JS.

    --
    All your base are belong to Wii.
  4. Re:As fast as C code??? by fozzy1015 · · Score: 2, Informative

    Correct me if I'm wrong, but generally speaking, I was always under the impression that, as an interpreted language, javascript will never be able to run 100% as fast as natively compiled C code.

    A real world Javascript program will rarely, if ever, be as fast as C. The idea with this is: Normally, loops, no matter how tight, are executed in the interpreter environment with all the overhead that it entails. The idea is to profile loops that are 'hot' enough to be compiled to machine code so that the following iterations don't have the overhead of the interpreter. The more iterations for a loop the potentially faster speedup. Since compiling in itself takes time obviously part of the profiling will be to determine if a loop is worth compiling or not.

  5. Re:Precursor to more of Firefox being in JS by Randle_Revar · · Score: 2, Informative

    JS drives most of GUI interaction in FF. The bulk of the C++ is down in gecko, for rendering speed.

  6. Re:Premature optimization.... by Randle_Revar · · Score: 3, Informative

    Like stupid simple multimedia.

    like SVG, <canvas> <audio> and <video>

  7. Re:As fast as C code??? by Rockoon · · Score: 5, Informative

    The amount of ignorance in this community is quite disturbing. I suspect that part of the cause is the unchecked pro-c religion, which always begins "C as faster than ...."

    Clue phone guys. Ring. Ring.

    C is a high level language and like all high level languages it can either be compiled, interpreted, or even translated. JavaScript is no different than C and can also also be compiled, interpreted, or even translated. There is nothing special about the C language that makes it inherently faster than other languages. C itself is a derivation of the language BCPL, whos shortcomming was a lack of datatypes. The design goal of these language syntaxes was parsing with minimal overhead because RAM was in short supply in those days. C was not designed to be "faster" than anything.

    C is commonly mistaken to be superior because the most popular C compilers are commonly more advanced than the compilers of other languages due to simple supply and demand metrics. C is more popular, so its compilers have traditionally gotten more development effort. C itself isnt special beyond its popularity.

    --
    "His name was James Damore."
  8. Fast as C but uses lots more memory by CustomDesigned · · Score: 5, Informative

    In general, JIT systems can really provide CPU performance near C speed, or even faster for some benchmarks, once the application gets going. The catch is that you pay two penalties: startup time and memory. Lots of memory: for keeping stats on what needs compiling, trampolines to call in and out of the interpreter vs. JIT native code, and the native code *plus* the byte code.

    Even dynamic languages like Python can have a JIT - it specializes a function for various combinations of argument types, and then provides a catchall generic version for general objects. The catchall version is not much faster than the interpreter, usually (in fact it could *be* the interpreter), but the specialized versions can be much, much faster. (Also blocks can be specialized within any of the function versions.) But all those specialized versions take up memory. So the JIT keeps stats and tries to make only the ones that really help.

    1. Re:Fast as C but uses lots more memory by Mithrandir · · Score: 5, Informative

      What's interesting is LLVM and .NET, where you can run C/C++ code in an interpreted/JIT-compiled environment. Potentially, with the optimizations mentioned above, you could have C code running in a virtual machine that's faster than statically-compiled C code.

      HP did a lot of research on this about 4-5 years ago where they proved this was exactly the case. Do some googling for HP's Dynamo project. Real runtime knowledge of exactly what is being used and when leads to better optimistaion than static optimisation.

      --
      Life is complete only for brief intervals in between toys or projects -- John Dalton
    2. Re:Fast as C but uses lots more memory by adonisv1 · · Score: 2, Informative

      You can look at Psyco which functions like a JIT for Python.

    3. Re:Fast as C but uses lots more memory by lubricated · · Score: 4, Informative

      So far JIT's just aren't as fast as C. They are very far along. People are using them for programs which may run for days at a time. This has more to do with having an easier time developing in a higher level language. In my experience java rivals and often surpasses C++ programs heavily reliant on the stl. Where I work, speed or features win marketshare and programs written in C are performance kings. Python and java are becoming more popular.

      --
      It has been statistically shown that helmets increase the risk of head injury.
    4. Re:Fast as C but uses lots more memory by Bj�rn · · Score: 4, Informative

      but never compared to C.

      Here is a somewhat old comparison of Java and C/C++ performance, done by the University of Southern California. Note that the article was written in 2003 and updated in 2004. Javas performance has improved significantly since then, most noticeably in JDK 1.6. This is the conclusion:

      Java is now nearly equal to (or faster than) C++ on low-level and numeric benchmarks.

      --
      Never express yourself more clearly than you are able to think. --Niels Bohr
    5. Re:Fast as C but uses lots more memory by Cyberax · · Score: 3, Informative

      "It wouldn't be JIT then, it'd have to be 'before the code is executed', because you wouldn't want the compiler to spring into action, run on core 2 whilst core 1 twiddles its thumbs waiting for the compilation to finish."

      For example, in Java HotSpot it _already_ works this way. A special thread compiles the code while other thread(s) work in interpreted mode. When compilation is finished, other thread(s) switch to this new fast compiled code.

      Also, there are optimizations in Java HotSpot which are _impossible_ to do using static analysis.

    6. Re:Fast as C but uses lots more memory by Cyberax · · Score: 4, Informative

      What a bunch of stupidity...

      First, "programs reliant on STL" can be as fast as anything on plain C. And very often they can be faster (for example, because dwarf-style exception handling is faster than return result checks).

      Second, JITs are NOT "very far along". HotSpot is _already_ faster than C/C++ on some benchmarks. And it's only going to get better, because HotSpot can use real runtime statistics to guide optimizations (C/C++ can also use Profile Guided Optimizations but it relies on static data).

    7. Re:Fast as C but uses lots more memory by Z00L00K · · Score: 2, Informative

      It compiled it into bytecode and references to variables, so it was reversible - just like Java.

      The references built was then used during execution as offsets into a jump table, which meant that it was fairly effective and the variables were referred by address to the definition area and not by name, which also did speed up things.

      See the link in my previous post for a few performance figures to compare with other systems of the time.

      The really boring thing with that computer was that the graphics wasn't really good, but you can't get everything. But compared to most computers of the time it was fairly competent and the successor the ABC800 which also used the same technique was in extensive use for business purposes in Sweden.

      It shall be noted that both computers were running a 3MHz Z80A (so it wasn't even using the full capacity of the Z80A for some reason!

      --
      If builders built buildings the way programmers wrote programs, then the first woodpecker would destroy civilization.
  9. Re:As fast as C code??? by cbrocious · · Score: 5, Informative

    Well, you're on the right trail, but not quite right. The key difference between Javascript and C, from an optimization standpoint, is the type system. The overhead of dynamic types is quite immense even at its most optimal. I suggest looking into the architecture of the DLR -- it really shows the key problems behind compiling dynamic languages and how they're being solved.

    --
    Disconnect and self-destruct, one bullet at a time.
  10. Re:Premature optimization.... by Anonymous Coward · · Score: 1, Informative

    It does rival Flash. They're working with Adobe on Tamarin, which is using the same core JS engine in Mozilla and Flash (ActionScript).

    The environment the JS runs in is different, of course. But the language itself... not so much. (They do have different parsers, though. They're different dialects of ECMA-262)

  11. Re:Premature optimization.... by Just+Some+Guy · · Score: 4, Informative

    intensive purposes

    "Intents and purposes." Somewhere, an English teacher cries.

    --
    Dewey, what part of this looks like authorities should be involved?
  12. Re:Performance is great and all by Anonymous Coward · · Score: 2, Informative

    I'm sure you could easily argue back

    I think I will

    Tied too much to the browser/Bad syntax

    Browser means it runs anywhere, syntax is C-like.

    No reflection.

    You can enumerate an object's methods, variables, etc as you can any other hash table.

    No dictionary/Adding stuff to Object (prototype.js)

    {} is a dictionary, no need to use Object. Even the author of prototype.js realized that adding methods to Object is a bad idea. I will complain that JS hash keys can only be strings.

    No threading. No locking. Nothing resembling concurrent programming. The more complicated your app the more arbitrary events and multithreading are important.

    setTimeout and setInterval are your friends. Cooperative multitasking makes some classes of problems more difficult, but greatly simplifies the language. There's quite a bit of infrastructure needed for correct behavior in the presence of preempting.

    Scoping. Scoping is mind-numbingly bad.

    Agreed. JS needs block level scoping like other sane languages.

    Namespaces (again, see library complaint) are implemented via object nesting, which isn't really namespaces

    It doesn't need to be called one to be one.

    Logging and debugging/Haven't used firebug/Creators weren't thinking about debugging

    If you haven't used Firebug, you are missing out! Debugging is no worse than other languages, and far better than IE's debugging support.

    Standard dialogs are alert() and confirm(). Anything and everything else you have to roll your own...

    Dialogs are modal... I think the absence of dialogs on the web encourages more usable alternatives.

    Browser identification and JS version identification. Why should I have to jump through hoops, poke & prod things, and guess at what my JS run-time is?

    Don't. Trigger off of capabilities rather than browsers. There may be a few instances where you have to specifically check for IE, but this is an exception rather than a rule.

    ... [JS is] a grossly inadequate language for rich application development...

    Besides the scoping gone horribly wrong, I can't agree with your conclusion.

  13. Re:Javascript is good by fimbulvetr · · Score: 2, Informative

    My wording was wrong - I did understand your paragraph and was merely trying to point that JS could be very helpful when doing SS'ing since it has native Dom parsers and methods. Indeed, there are many things in JS that lend itself well to being able to "read itself" - see Douglas Crockford's comments on JSLint, for instance.

  14. Re:The Greatest Idea by Anonymous Coward · · Score: 2, Informative

    Ever since the matrix, it's been green text on black background. That movie was 10 years ago, get with the times man!

  15. Re:Premature optimization.... by shreevatsa · · Score: 2, Informative

    is the root of all evil. --C. A. R. Hoare

    No, Donald Knuth, Structured Programming with go to Statements, ACM Computing Surveys, Vol 6, No. 4, Dec. 1974 (p.268). (Unless you can find a reference for Hoare having said it. See here.)

  16. Re:As fast as C code??? by shaitand · · Score: 3, Informative

    Java ByteCode is NOT equal to C in speed in the real world. C# applications are NOT equal to C in speed in the real world. For that matter, C++ isn't even equal to C speed in the real world (although the margin is very slim if both are optimized correctly and C++ will work most anywhere that asm shouldn't be used IMHO).

    That said, speed is not normally the most critical thing in an application. All of the above mentioned languages have strengths of their own and perform well when used properly.

  17. Re:As fast as C code??? by arth1 · · Score: 2, Informative

    With JIT, what typically happens is that everything is interpretted at first, and the runtime compiler begins to analyze which code segments should be optimized. Because the compiler knows not just the source material, but how it's actually running it can do a better job optimizing the code than native C compilers could.

    It's not that simple. A native compiler can do profiling as part of the compilation, and spend quite a bit of time on branch prediction and other time consuming optimizations. Five seconds extra while compiling natively might not be a problem, but five seconds extra when JIT compiling might be unbearable.

    However, where languages like javascript, java and C# will always lose the most is in preemptive task switching, where the overhead is humongous compared to the native pre-compiled code, and you'll end up not just invalidating L1 cache but L2 (and great parts of L3 too where present).

    Another loss is the lack of link time optimizations. Two identical static functions can not be identified and merged. Nor can the functions be reordered to avoid page switching. Nor can virtual address mapping be done ahead of time (like SGI's requickstart and Linux' simpler prelink).

    JIT can du run-time profiling with valid data and genuine IO? So can native compiled code. It's just more work to set it up, but it's not rocket science either.

    Yes, you can get JIT code that approaches compiled code, but only for special cases, and the price is both a much higher memory use (which can negatively impact other tasks you may have running) and increased startup time. But I have yet to see bytecode or JIT that runs faster unless the tester does some blatant cheating to get the desired results (e.g. by using different algorithms, like comparing perl's using hash tables to a linear search in C, or by disabling all optimizations in the native compiler but keeping them in the JIT compiler).

  18. Re:As fast as C code??? by beelsebob · · Score: 4, Informative

    Except that C# code isn't as fast as C code. It's 2.7 times slower.

  19. Re:Java/C#/C++/C equally fast by makomk · · Score: 2, Informative

    IIRC, it's possible for the same program to be faster when compiled using the C++ mode of the compiler than the C mode, due to C++ being stricter and allowing some optimisations that aren't safe in C. Don't quote me on this, though.

  20. Re:As fast as C code??? by hattig · · Score: 4, Informative

    1) That's the Mono implementation of C#

    2) Intel's C++ compiler is known to be one of the best but cannot handle all situations. GCC's takes 1.3 times longer. Java 'only' takes 1.96 times longer.

    2a) Intel's C++ compiler got 3 errors in the benchmarks you linked to, thus it should have been disqualified. The Intel C got 1 error and a "no program", so again, disqualified. The first compiler to run all the tests was in fact the Gnu C++ compiler.

    Ruby at 63x longer (or 30x slower than Java) should shut up all the Java haters who witter on about Ruby (or PHP at 12 slower than Java) for web development.

  21. Re:As fast as C code??? by tkinnun0 · · Score: 3, Informative

    Another loss is the lack of link time optimizations. Two identical static functions can not be identified and merged.

    Sure they can. The JVM can calculate a hash of each method's code as it loads classes, then change constant pool entries to point to the canonical version of the merged method.

    Nor can the functions be reordered to avoid page switching.

    Sure they can. The code is just data and the JVM already does heap compacting.

    Nor can virtual address mapping be done ahead of time (like SGI's requickstart and Linux' simpler prelink).

    Why would you want to do that? As I understand, the more choices you can defer until you have "boots on the ground", the better.

  22. Re:The Greatest Idea by gbjbaanb · · Score: 2, Informative

    tip: don't use Task Manager, for investigating performance issues, use PerfMon (under administrative tools on your start menu).

    Open a performance report, add the %CPU counter for just IE (it'll be under the Process counter set).

    You can get it to update faster than 1 second default, and get it to log to a file instead so you can view it later. Add a network counter and memory counter too for more insight.

  23. Re:Performance is great and all by MrMunkey · · Score: 2, Informative

    At least IE natively will let you debug JS!

    That's the first I've heard of that. In order to debug JavaScript you either have to have Office installed, or download the MS Script Debugger. IE does report errors, but their description is horrible. I've found that just the error console in Firefox is much, much better than IE's error reporting. You really should look into Firebug. It's invaluable

  24. Re:Performance is great and all by Anonymous Coward · · Score: 1, Informative

    No dictionary. Sure, you can use an Object as a dictionary but the second someone prototypes it to add root functionality then you've introduced other items in your "dictionary".

    Just use hasOwnProperty.

    for (var i in myDict) if (myDict.hasOwnProperty(i)) {
        doStuffWith(myDict[i]);
    }