Slashdot Mirror


Compiling to JavaScript: TypeScript vs. Haxe

lars_doucet writes: Released in 2012, Microsoft's TypeScript is perhaps the best-known "compile to JS" language, but it wasn't the first. One of the earliest was Haxe, whose JS target first appeared in 2006. In his illuminating article, TypeScript vs Haxe, Andy Li gives an excellent rundown of the two languages' various merits, but the bottom line is: "Existing JS developers will favor TypeScript as they are more similar in many ways. They can utilize their existing skills immediately. Non-JS developers with backgrounds like Java/C# or even from the functional programming world will appreciate Haxe more since it fixes a lot of weirdness of JS." The full article includes an excellent rundown of the type systems, syntax, scope handling, compilers, and overall language design philosophy.

18 of 94 comments (clear)

  1. Dear lord why by Anonymous Coward · · Score: 4, Funny

    Compiling to JS is like having a shit for dinner, you're doing it wrong.

    1. Re:Dear lord why by spamdog · · Score: 2, Insightful

      Oh come on. Shitting on javascript is timeless.

    2. Re:Dear lord why by jandersen · · Score: 2

      Compiling to JS is like having a shit for dinner, you're doing it wrong.

      What are you talking about? 50 million flies can't be wrong.

    3. Re:Dear lord why by Anonymous Coward · · Score: 3, Insightful

      How is this post marked flamebait but not the first original one?
      Javascript is proving to be one of the most widespread and widely adopted runtimes in the world.
      It is cashing cheques that java's ego wrote out.

      Compiling to JS is in fact the way most strong typed addicts are able to write javascript without losing their sanity.

      Mod Anonymous Coward down to flamebait and leptons to informative please!

  2. Compile to JS vs WebASM by Dutch+Gun · · Score: 4, Interesting

    How is that web assembly project coming along? It seems like a perfect fit for alternative languages like this instead of having to compile to JS. I think it will be a nice day when developers can choose a web language based on its merits rather than its ubiquitous nature.

    --
    Irony: Agile development has too much intertia to be abandoned now.
    1. Re:Compile to JS vs WebASM by thisisauniqueid · · Score: 4, Informative

      How is that web assembly project coming along? It seems like a perfect fit for alternative languages like this instead of having to compile to JS.

      WebAssembly is not currently a suitable target for Javascript compilation, because it lacks a garbage collector. Only languages that can base themselves on (the equivalent of) malloc/free can currently target wasm. Eventually garbage collection facilities will be provided, but they're not on the immediate drawing board, and even once garbage collection is possible, it's likely to be a long time before JS->wasm approaches the performance of v8 or similar. (Note that asm.js doesn't do garbage collection either.)

      The nice things about wasm are (1) it gives the creators a chance to re-invent the web runtime from the ground up (e.g. native threads will be there almost from the start), (2) languages can evolve independently of the web platform, and the web platform becomes language agnostic, and (3) *all* the major players are fully on-board.

      One of the nicest features of wasm, in my opinion, is that the intermediate representation is an AST of expressions to be computed. This is a far more sensible and flexible IR than some stack-based or register-based VM bytecode system. My prediction is that wasm will become the preferred compilation target for desktop/server applications, not just web applications: the same toolchain will be used to generate binaries for running in server containers as well as within a browser.

    2. Re:Compile to JS vs WebASM by thisisauniqueid · · Score: 3, Informative

      What's actually wrong with Javascript as a compile target?

      Where to even begin... maybe the fact that Javascript doesn't even have integers? Or that (other than in asm.js) it doesn't support C-style malloc/free heap-based allocation? Or that (again, other than in asm.js) the runtime is garbage collected? Or that it doesn't properly support threads (a huge deal given the demands of modern computing), or sharing of memory between WebWorkers and the main thread?

      Sure you can argue the problems with the language itself but when it's a compile target all of those are abstracted away, the thing that is important then is speed. And it's not exactly slow, could it be faster? Most likely. Does it need to be? For most cases probably not.

      Javascript wastes petawatt-hours of energy per year across the globe due to its computational inefficiency. That means your power bill is higher than it could be, your phone battery doesn't last as long as it could, and polar bears are dying in the Arctic because Javascript has been deemed "efficient enough".

      UI latency irks our subconscious. Once we have native WebAssembly support in browsers, and a significant percentage of the major websites have moved across to compiling from better languages into WebAssembly, those new sites are going to feel amazingly snappy, and people aren't going to want to use old Javascript sites, because they'll feel janky.

    3. Re:Compile to JS vs WebASM by serviscope_minor · · Score: 3, Insightful

      Once we have native WebAssembly support in browsers, and a significant percentage of the major websites have moved across to compiling from better languages into WebAssembly, those new sites are going to feel amazingly snappy, and people aren't going to want to use old Javascript sites, because they'll feel janky.

      Aaah you are in good fooling this morning, sir!

      Given that (a) computers have got vastly faster and (b) javascript engines have got vastly faster and (c) websites keep getting slower and slower, I somehow suspect that the future is not going to be a bright one like the distant past where UI lag was a rare thing.

      Here's another thing that irks me. The old "obsolete" X11 style was to have sub-sub-sub windows for damn near everything. Every UI element was in it's own subwindow. Subwindows were on the server, so when you clicked the mouse, it knew which subwindow you hit and sent that message. The modern, not "obsolete" style is to have one window and use pixel addressing.

      The former works much, much better in a world of UI and network lag. The common latter means you have the lovely thing where you press, the UI jumps and then processes the click afterwards so you hit the wrong place with sometimes hilaroius concepts.

      What's kind of funny is the web is slowly slouching towards a dubious re-inventing of the past 50 years of computing without bothering to learn anything about the old mistakes.

      Now I'm going to go back to poking badly documented registers on 4k 8051 microcontroller because frankly the web "technologies" makes that pile of insanity look good.

      --
      SJW n. One who posts facts.
    4. Re:Compile to JS vs WebASM by aaaaaaargh! · · Score: 2

      Here are my 2 cents on this "web thing":

      The language doesn't matter, the framework matters. I couldn't care less which language I use as long as the APIs are reasonable. But if I have to start considering abhorrences like HTML, encoding data into URLs, "cookies" and dealing with the intricacies of client/server responses, then my reaction is "no, thanks" - unless I'm forced to use the framework anyway.

      In other words, your language could be the shittiest possible and I'd still be fine with it as long as it allows me to write ordinary GUI apps with a reasonable gadget-based API that can be packed and deployed into browser-based applications easily. Most frameworks I've seen are simply too full of web-monkey crap. I don't want to fiddle around with lines and lines of boilerplate that has nothing to do with application development.

    5. Re:Compile to JS vs WebASM by aaaaaaargh! · · Score: 3, Interesting

      What's even worse is that people are piling up layers after layers without really caring about bug ratios and error tolerance. It's like building a skyscraper out of parts that all have a little bit of structural damage and saying each time: "Nah, that little hairline crack won't matter. I'm fairly confident that the whole thing will hold."

    6. Re: Compile to JS vs WebASM by terjeber · · Score: 2

      Once we have native WebAssembly support in browsers

      ...six months after Linux wins the desktop war... In the mean time, for those of us who need to deliver solutions today, Typescript makes Javascript palatable.

  3. If I had to compile to JS I'd look at emscripten by istartedi · · Score: 3, Interesting

    If I had to compile to JS I like to think I could use emscripten, but not being involved in such a project I'm not sure how well it works. When it's supported by the browser, it's designed to strip away most of the layers of crap we've put between ourselves and the machine. IIRC, you can get half the speed of native C by running C compiled to JS this way. Once again though, the browser has to support it. I think in theory it can support any language that LLVM supports on the front end; but I think they've only tested it with C and C++.

    Anyway, if you're starting a new project I don't see why you'd want to use a language designed just for compiling to JS when you could use something more general purpose that will potentially run very fast with the proper compiler.

    --
    For all intensive purposes, "whom" is no longer a word. That begs the question, "who cares"?
  4. Re:eeeeeexcellent by thisisauniqueid · · Score: 2

    Haxe isn't very high-profile, but Typescript is an important language, if only for the fact that it is starting to drive the design of ECMAScript. Typescript has recently become a strict superset of ECMAScript functionality, and Typescript has become the new testing ground for features before they get incorporated into ECMAScript. (This may not always be true, because Typescript is developed by Microsoft, but it seems to be true at least for now. You know to sit up and start paying attention when Google decides to rebase one of their strategic platforms -- Angular -- on a Microsoft technology.)

  5. Re:eeeeeexcellent by thisisauniqueid · · Score: 3, Informative

    Um, I know that ECMAScript "IS" Javascript. Typescript is becoming much bigger than Microsoft itself. The only Microsoft products I have ever been interested in using before Typescript are their keyboards and mice. Typescript is, quite shockingly, actually really, really good, and Microsoft, even more shockingly, is suddenly becoming a really good citizen in the open source / Free software world, at least as far as some projects are concerned, and the stuff they're putting out there is really good. (See also: Visual Studio Code.)

    Microsoft have also, quite suddenly and inexplicably, become extremely open about the development process of some of these newer open source projects, and they are working with, and responding to, other key players in the open source ecosystem. (Google adopted Typescript for Angular2, and Microsoft merged Angular's "AtScript" extensions into Typescript, and made Typescript a strict superset of ECMAScript 7, in order to satisfy Google's requirements, and to make Typescript a better player in the Javascript world. Microsoft was a key presenter at, and participant in, ng-conf 2015. etc. etc.

  6. Other languages targeting the JS runtime by geggo98 · · Score: 2

    There are also PureScript, Scala.js and of course ClojureScript.

  7. Not that bad by monkeyxpress · · Score: 4, Insightful

    I know this is going to get a lot of hate, but JavaScript is really not as bad as people make out. There are some stupid design decisions (mostly around scoping and built-in type consistency) but once you know what these are it is pretty easy to work with them. The main problem I think most people have is that it does not really offer any sort of hand-holding in terms of how you should structure your program. But in a way it is quite beautiful how you can create usable frameworks for OO, imperative, or functional programming with the same language. The problem I mostly see is that people get stuck with a poor design decision, but rather than having to re-factor their object structure (like C# would force you to do), they can just hack in a solution that breaks basically every principle of good programming imaginable. They then dump this solution on the world and declare it is because JavaScript sucks. The real issue is that they are bad programmers who came up with a poor design and then used the (somewhat excessive) flexibility of JavaScript to get them out of a corner. Same thing has happened in C/C++ for years.

    I really don't think JavaScript should have become the language of the web, but competent programmers shouldn't listen to all the hate that gets spread around. It is a decent way to introduce yourself to a lot of new programming concepts if you've come from a static OO language background. Once you understand some fundamentals CoffeeScript, TypeScript etc are pretty straight forward and their applicability is obvious.

    For what it's worth, if you are doing large programs then something like TypeScript can be very useful. The problem it solves is really an issue with dynamic languages in general though, rather than JavaScript. There are ways to create large maintainable code bases with a dynamic prototypical language, but sometimes the classic OO model just fits a problem better.

    1. Re:Not that bad by JustAnotherOldGuy · · Score: 2

      I agree- although javascript has some bad design flaws, it's actually an enormously useful tool for which there is no real replacement.

      For example, how else would one do any kind of dynamic page manipulation? Yes, JS has flaws, but it works fine for what it does. It was never meant to be used to create huge, enterprise applications...I mean, who would write (for example) a medical billing application in JS? No one, because there are better languages for that. But for client-side page manipulation and calculation, JS is the best (and probably only) choice.

      --
      Just cruising through this digital world at 33 1/3 rpm...
  8. OTish: same lang to write an OS and website logic? by rodia · · Score: 2

    If you think this is would be a good thing, try Nim, formerly known as Nimrod. Abstraction level between C and C++ and it compiles to JS. Apparently carries less runtime baggage than emscripten.