TypeScript 7 Officially Released And Boy Is It Fast
BBetter Stack
Computing/SoftwareInternet Technology
Transcript
00:00:00typescript 7 has officially been released and after more than a year of painstaking development
00:00:05porting the compilers go bun proved that you can actually just do the entire thing with ai so i'm
00:00:10sure the developers are happy about that if you now run npm install typescript you will get version
00:00:167 which is the version with the go compiler and following the announcement video from microsoft
00:00:20a couple of days ago i wanted to dive into exactly how they've achieved these massive performance
00:00:25gains the results have been genuinely impressive the new compiler builds the vs code code base
00:00:31which is 1.3 million lines of code across almost 8 000 files in just 10 seconds compared to the 125
00:00:39seconds from the old compiler and with typescript now proudly sitting as the number one language on
00:00:44github believe it or not the new compiler is going to change so many lives i'm just getting emotional
00:00:49thinking about it but rather than look at the almost identical features between 6 and 7 i wanted to dive
00:00:54into exactly how that performance is achieved so let's take a look at the new compiler and see what
00:00:59makes it so fast so why the rewrite well the creator of c-sharp and technical fellow at microsoft
00:01:09anders heilsberg said it quite plainly javascript is optimized for ui and browsers it's not really
00:01:16optimized for compute intensive workflows and compilers which is pretty obvious why because it's single
00:01:22for threaded and processing something like an abstract syntax tree or type checking can only really scale
00:01:27so far with access to a single core you could technically push work across workers but then you'd
00:01:32need to serialize and deserialize the data which is slow and memory intensive and then you'd have to
00:01:38deal with the complexity of managing all of that so they settled with go which provides dramatic
00:01:43performance improvements using a language that is basically fit for purpose go is a compiled language
00:01:48which means we can run compiled code directly on the cpu rather than having to interpret it and go has a
00:01:54very good concurrency model which means we can easily run multiple threads of execution at the same time
00:02:00all with shared memory this means we can take advantage of all the cores on your machine rather than just
00:02:06one and there's roughly an even split up to half the speed up is attributed to being in native code
00:02:12and the rest from having shared memory concurrency go is basically the perfect language for this
00:02:17it's not just that go is faster it literally has more resources to throw out the problem and the results
00:02:23speak for themselves if we look at the numbers vs code at 1.3 million lines of code came in 125.7 seconds on the old
00:02:32compiler 10.6 seconds which is 11.9x improvement for blue sky we've got 24.3 to 2.8 that's 8.7 improvement
00:02:42and then for playwright we have 12.8 seconds down to 1.5 and 8.7 improvement and funnily enough it only
00:02:49takes one second to subscribe to better stack now the gap here is only going to widen because cores are not
00:02:54really getting faster at the same rate they used to but what we do get is more cores i'm on an m3 max for
00:03:00example which has 14 cores which is insane i remember building a gaming pc back in college which had an
00:03:06intel i7 with four cores and i thought that was an absolute beast the javascript compiler would just
00:03:11use one of those cores but with go i unlock all of them and of course the more cores you have the more
00:03:17performance you get if i compile a large project on this machine in version 6 you'll see it takes 45
00:03:24seconds and with version 7 we can get that down to as little as three seconds and there are multiple
00:03:29phases to the compilation process and the type checking phase is just one of them for this go
00:03:34will spin up four type checkers and have them check a quarter of the code base each but you can actually
00:03:39improve that even further so during compilation you could set checkers to 12 for even faster performance
00:03:45so i've downloaded the vs code repo to my machine so we can see the difference between typescript 7 and 6
00:03:51by default tsc on my machine will now point to typescript 7 so we'll just run the diagnostics on
00:03:57this and you can see that took 5.4 seconds and the majority of the time was actually spent doing the
00:04:02check time so 4.7 of those seconds we can actually speed that up as well so if we add another flag
00:04:08checkers 12 so you can see the total time now has gone down from 5.4 to 3.5 and all of that shaves
00:04:15off the check time so here we've got 4.7 and then that's gone down to 2.9 now let's run the same
00:04:20command again but this time we're going to be using typescript 6 and this is going to take a little
00:04:23while so we'll speed through it so after waiting for that you can see that typescript 6 finished with
00:04:2845.3 seconds compared to 3.5 that we get when we run across all the cores so that is effectively
00:04:36a 15x improvement on my m3 max chip so for me this is where the 3.5 seconds actually comes from doing
00:04:43this will of course steal performance from other processes but if you're not doing anything then
00:04:47you may as well use all the resources that your machine has so let's take a look at some real code
00:04:52from the new compiler to understand how go is able to achieve this performance here we have a bind source
00:04:57files function whose job is to build up every declaration in a file and work out what scope it belongs to
00:05:03we're processing possibly thousands of files we loop over each file we queue up a function to bind it
00:05:09then we wait for all of them to finish the beautiful part is that we don't have to think about how that
00:05:14work gets distributed across cores goes runtime just handles all of that we're not spawning threads or
00:05:20managing complex communication between cores we just say here's some work and it figures out how to do the
00:05:26rest in javascript we could write almost identical code but this is pointless for cpu-bound work it
00:05:32would all still just happen on one thread in sequence even if promisor gives the illusion of parallelism
00:05:38workers are technically away around this but you can't share objects between them only raw bytes with
00:05:43shared array buffer so handing a past syntax tree to a worker means serializing the whole thing and
00:05:48copying it across and rebuilding it on the other side for a big file that can cost more than the work itself
00:05:53basically it's stuff like this that makes go inherently better for cpu-bound work it has a
00:05:58runtime that can handle all of this concurrency for you and it has shared memory so you can pass objects
00:06:03around without having to copy them now aside from the compile time the thing you're going to notice the
00:06:07most is the speed of your language server anyone who's worked on a large typescript code base knows the
00:06:13pain of waiting for types to be type checked and my god these intel max really felt this i remember working on
00:06:20projects a couple of years ago and you would open the repository and literally have to wait
00:06:24up to two minutes just to see the squiggly lines come in and then every time that you made a change
00:06:29it would just be incredibly painful the developer experience is terrible and then no one wants to
00:06:34work on the code base the new language server however means instant feedback in your ide so you can
00:06:40open a file make a change and see the errors in literally milliseconds even if you're working on
00:06:46potentially massive code bases aside from performance the new language server is now more stable so the
00:06:51need to restart your ide when the type checking just stops working is drastically reduced typescript 7
00:06:57reduced failing language server commands by over 80 percent and reduced server crashes by over 60
00:07:04so that's less laptops thrown at walls which is actually pretty good for the environment so
00:07:08who knew microsoft actually cared about the planet now it's also worth mentioning this is a port
00:07:13not a rewriter the typescript team have been very careful to ensure that the new compiler is basically
00:07:19fully compatible with the old one you likely won't even notice a difference except for the speed but
00:07:24whilst you can now run typescript 7 on your own projects you'll still need to wait for your favorite
00:07:29packages to catch up their programmatic api is still missing which means that any package that depends on
00:07:35that will need to wait for the 7.1 release so packages like typescript es lint ts jest or ts node will
00:07:42lag behind now the full release of typescript 7 is available for you to download but you do
00:07:47need to explicitly install the typescript 7 extension for vs code the default package will eventually be
00:07:53updated but for now just install that extension it's called typescript 7 on the extension store and
00:07:58then everything will run as expected if you want to see more about the feature set of typescript 7 we've
00:08:03filmed a video on exactly that that you can see here and if you enjoy breakdowns like this then subscribe
00:08:08to better stack for more so hopefully you learned something there and now you can take advantage of
00:08:12the rapid development that you're going to get with typescript 7 i definitely know i'm going to enjoy
00:08:16using this one but thank you for watching and of course i'll see you in the next one