Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which version of JavaScript should I compile to? [closed]

Let's say I'm writing a NodeJS project with a language that compiles to JavaScript, like TypeScript for example. With TypeScript (or other things that use Babel) I have the option to compile my code to any version of JavaScript, be it ES3, ES5, ES6, ES2017, etc.

Obviously, if I was writing something client-side, I'd stick to an earlier version like ES5 to maintain compatibility for users. However, since I'm writing something server-side specifically for the latest version of NodeJS, I could use any of these options. What I want to know is, which version should I use to help provide the best performance?

NodeJS's interpreter, V8, provides different optimizations depending on how the source code is written, so I would imagine that selecting one or the other may result in different benefits. On the one hand, you would think that selecting an older version of JS would be better since its been around longer and has had more time to be optimized. On the other hand, compiling to later versions of JS results in significantly cleaner code and relies upon newer, available features, which may also help.

Any thoughts / feedback would be appreciated!

like image 737
Griffort Avatar asked Aug 27 '18 08:08

Griffort


1 Answers

I do believe this question to be primarily opinion based, but.. I don't think a valid question should be ignored, just because there is a part that may be considered subjective. So while you should take this with a grain of salt (because ultimately, it is your application and your choice what version you use), I do hope this should nudge you in the right direction:


I personally am of belief, that you should compile to the latest ES version. Why?
First, let's go over the facts:

  • Compatibility is not that big of an issue. Many of the older features can be polyfilled for compatibility.
  • As @Ry- validly pointed out, when a feature gets converted to older JS version, it can behave differently, often times in ways you may not even notice or expect.
  • With every ES version there are some QoL changes, bugfixes and code improvements
  • Provides new useful features

And now, this is more of a personal opinion, but I stand by it:

  • I don't think we as developers should look back and use obsolete or downright non-functioning techniques and bash our heads against the wall. Instead of complaining about others not wanting to adapt, be part of the movement and make others adapt. I'm not saying your site should not be "fool-proof" for old browsers, quite the opposite, just don't get down to their level and limit yourself because of it. Go with the flow!

So to sum it up, unless you have a very particular reason, as to why you shouldn't use the latest version, then always use the latest version (LTS, not latest beta).

like image 56
Samuel Hulla Avatar answered Nov 09 '22 11:11

Samuel Hulla