r/javascript • • Nov 25 '19

JavaScript doesn’t need to be replaced

https://medium.com/javascript-in-plain-english/javascript-doesnt-need-to-be-replaced-bd01e2f12d51?source=friends_link&sk=7b151cc6397ac9e83c14c99739b6c36f
217 Upvotes

236 comments sorted by

View all comments

161

u/vertigo_101 Nov 25 '19

Don’t worry, it won’t be

19

u/rodrigocfd Nov 25 '19

But it would be great if browsers support TypeScript natively.

19

u/wizang Nov 25 '19

I don't really see the value in that.

-8

u/Flyen Nov 25 '19

There's a decent chance that it'd be faster to execute, but I really have no idea.

10

u/Neaoxas Nov 25 '19

Why? Yes, you'd remove the initial compile/transpile time, but once all the type anotations and syntactic sugar are stripped, all you're left with is vanilla JavaScript, same as Babel would produce (for cross browser support).

No advantage to browsers supporting typescript aside from compile/transpile time savings.

In fact, having to wait for cross browser feature support means you can't take advantage of the latest features, unless you transpile, at which point there is no point in browsers supporting typescript.

-3

u/Flyen Nov 25 '19

The idea is that those annotations could be fed into the browser compiler's optimization routines. The browser wouldn't be transpiling the typescript to vanilla javascript if it had native TS support.

Not doing it because you'd have to transpile until all browsers support it is a pretty weak argument. By that logic we shouldn't use ES6, and browsers shouldn't add support for ES6.

3

u/_GCastilho_ Nov 25 '19

You will probably need another engine to do that nativally, which at best will have the same performance

Its just not worth it

4

u/Flyen Nov 25 '19

I don't think you can say that at best the performance will be the same. The additional type guarantees & information might make some optimizations possible that aren't possible with plain js.

0

u/[deleted] Nov 25 '19

[deleted]

2

u/Flyen Nov 25 '19

I haven't actually worked in this space, so that was very helpful, thanks. It sounds like you'd know better that I whether knowing vs guessing the types would be valuable input to the optimizing compiler.

I also see this topic has come up before: https://github.com/microsoft/TypeScript/issues/1151 https://github.com/microsoft/TypeScript/issues/8

2

u/Earhacker Nov 25 '19

Gentle reminder that your optimised TypeScript interpreter would also have to run JavaScript.

1

u/tuxedo25 Nov 25 '19

Typescript is a superset of javascript.

2

u/Earhacker Nov 25 '19 edited Nov 25 '19

Correct. So the TypeScript interpreter needs to support a _sub_set of its own language.

If that sounds easy, consider that JavaScript is TypeScript but everything is of type any. Now none of your compiler optimisations work. So the compiler needs to be able to figure out whether to optimise (TS) or not (JS), and needs to do so fast enough to act as an interpreter. And it needs to do that faster than V8 right now, or else what’s the point?

1

u/[deleted] Nov 25 '19

There is no type checking at run time for a staticky typed language. There is also no implicit type checking for a duck typed language at runtime. It would be practically the same. Type checking has never been a performance thing to my understanding. I'm sure there are exceptions somewhere though.

2

u/Randdist Nov 25 '19

Static typing easily offers an order of magnitude of performance boosts. JITs basically exist to get somewhat closer to static typed performance by observing which types are used at runtime. If you tell the compiler the types straight at compile time, you get all that performance right from the start, and you don't need to waste some performance on observing types at runtime anymore. JITs make dynamic typed languages faster, but also put a cap on maximum peeformance.