Now Reading
The Saga of the Closure Compiler, and Why TypeScript Gained

The Saga of the Closure Compiler, and Why TypeScript Gained

2023-09-28 02:55:38

This is one thing that makes me really feel outdated: in simply six months, Gmail will rejoice its twentieth anniversary. Should you weren’t actively growing web pages on the time, it is laborious to seize simply how revolutionary it was. This was a time when JavaScript was held in nearly universally low regard. The concept that you may construct a complicated internet app utilizing it was mind-boggling. Nevertheless it clearly labored and it heralded the daybreak of the single-page internet app (SPA).

Behind this software was an thrilling new device that Google had constructed for creating giant JavaScript functions: the Closure Tools (that is Closure with an ‘s’, not Clojure with a ‘j’, which is a distinct factor). This included the Closure Compiler (CC), a JavaScript source-to-source compiler that did kind checking. Sound acquainted?

Except you have labored on frontend at Google sooner or later up to now 20 years, it is unlikely that you have ever encountered the Closure Compiler. It occupied an identical area of interest to TypeScript, however TypeScript has completely, definitively received.

Nonetheless, it is attention-grabbing to revisit CC for a couple of causes:

  1. By taking a look at a system that made totally different high-level design choices than TypeScript, we are able to achieve a deeper appreciation of TypeScript’s design.
  2. It reveals us lacking options from TypeScript that it may not have even occurred to us to need.
  3. It is an attention-grabbing case examine within the historical past of JavaScript.

In different phrases, the saga of the Closure Compiler offers us some perspective. TypeScript has change into so ubiquitous that it is generally laborious to think about some other approach of including a kind checker to JavaScript. The Closure Compiler reveals us that the design house was bigger than it appears to be like looking back.

I wrote Closure-style JavaScript at Google most closely from 2012–14. This publish displays Closure because it existed at that time. I am a lot much less aware of the way it’s advanced since then.

What’s the Closure Compiler?

TypeScript’s motto is “TypeScript is a superset of JavaScript”. Closure code, then again, is JavaScript. It does not add any new syntax to the language.

Should you’ve ever used TypeScript with --checkJs, it is a related thought. Quite than including sorts to JavaScript via new syntax, you add them through JSDoc-style feedback.

Evaluate this TypeScript:

perform max(a: quantity, b: quantity): quantity {
return a > b ? a : b;

to the equal Closurized JavaScript:

perform max(a, b) {
return a > b ? a : b;

An invalid invocation of max will end in an error:

> google-closure-compiler "--warning_level" "VERBOSE" "max.js"

max.js:12:16: WARNING - [JSC_TYPE_MISMATCH] precise parameter 1 of max doesn't match formal parameter
discovered : string
required: quantity
12| console.log(max('foo', 'bar'));

max.js:12:23: WARNING - [JSC_TYPE_MISMATCH] precise parameter 2 of max doesn't match formal parameter
discovered : string
required: quantity
12| console.log(max('foo', 'bar'));

0 error(s), 2 warning(s), 100.0% typed
perform max(a,b){return a>b?a:b}console.log(max("foo","bar"));

That is much like what tsc does in some methods however totally different in others. Similar to tsc, it experiences kind errors in your code. And similar to tsc, it outputs JavaScript (the final line). At a excessive stage, kind checking and JS emit are additionally the 2 issues that TypeScript does.

There are some attention-grabbing variations, too. The Closure Compiler experiences that our code is “100.0% typed”. Utilizing TypeScript terminology, it is a measure of what number of any sorts you will have. (Effective TypeScript discusses utilizing the type-coverage device to get this info in Merchandise 44: Observe Your Sort Protection to Forestall Regressions in Sort Security.)

The opposite attention-grabbing distinction is that the output is minified. This will get us the basic design aim of the Closure Compiler: producing the smallest JavaScript attainable.

Minification as Design Objective

When Gmail got here out again in 2004, community speeds have been a lot, a lot slower than they’re as we speak. The Gmail workforce discovered that runtime JavaScript efficiency was nearly irrelevant in comparison with obtain occasions. Should you wished to make your web page load quicker, you wanted to make your JavaScript bundle smaller. So that is the central aim of the Closure Compiler and its “superior optimizations” mode.

To see how this works, let’s take a look at some code to fetch and course of knowledge from the community.

This is an “externs” file (the CC equal of a kind declarations file) that defines a kind and declares a perform:

let APIResponse;

perform fetchData() {}

Some attention-grabbing issues to notice right here:

  • Sorts are launched through @typedef in a JSDoc remark. The APIResponse image exists at runtime however is just not notably helpful. Simply because CC is JavaScript doesn’t suggest that the JavaScript all the time is smart.
  • The declaration of fetchData consists of an empty implementation. TypeScript would use declare perform right here, however this isn’t JS syntax. So CC makes use of an empty perform physique.

This is some extra code that fetches knowledge and processes it:

let ProcessedData;

perform processData(knowledge) {
return {

const apiData = fetchData();
const processedData = processData(apiData);
console.log(processedData.longPropertyName, processedData.anotherLongName);

As a result of it is simply JavaScript, this code will be executed straight, presumably through a <script> tag (CC predates Node.js). No construct step is required and your iteration cycle may be very tight.

Let’s take a look at what occurs if you compile this:

> google-closure-compiler "--warning_level" "VERBOSE" "--externs" "api-externs.js" "api.js"

let ProcessedData;perform processData(a){return{,}}const apiData=fetchData(),processedData=processData(apiData);console.log(processedData.longPropertyName,processedData.anotherLongName);

This is what that appears like after we unminify it:

let ProcessedData;

perform processData(a) {
return {

const apiData = fetchData(), processedData = processData(apiData);

console.log(processedData.longPropertyName, processedData.anotherLongName);

Similar to TypeScript, compilation right here principally consists of stripping out kind info (on this case JSDoc feedback).

Now take a look at what occurs after we activate “superior optimizations”:

> google-closure-compiler "--compilation_level" "ADVANCED" "--warning_level" "VERBOSE" "--externs" "api-externs.js" "api.js"

var a,b=fetchData();a={,};console.log(a.h,a.g);

The output is a lot shorter. This is what it appears to be like like unminified:

var a, b = fetchData();

a = {

console.log(a.h, a.g);

It is a radical transformation of our authentic code. Along with mangling our variable names (apiData turned b, processedData turned a), the Closure Compiler has mangled property names on ProcessedData (longPropertyNameh, anotherLongNameg) and inlined the decision to processData, which let it take away that perform completely.

The outcomes are dramatic. Whereas the minified code with easy optimizations was 231 bytes, the code with superior optimizations is simply 62 bytes!

Discover that CC has preserved some symbols: the fetchData perform and the foo and bar property names. The rule is that symbols in an “externs” file are externally seen and can’t be modified, whereas the symbols elsewhere are inner and will be mangled or inlined as CC sees match.

That is basically not like something that TypeScript does. TypeScript doesn’t rename symbols when it emits JavaScript nor does it try to minify your code. Even should you run your generated JavaScript via a minifier, it will not do something practically this radical. It is laborious (or not possible) for a minifier to know which symbols or property names are a part of an exterior API. So mangling property names is usually unsafe. You are unlikely to get something smaller than the 231 byte “easy optimizations” output with TypeScript.

These outcomes usually maintain up properly after gzip compression, and in bigger initiatives as properly. I ported a JavaScript library to Closure in 2013 and shrank my bundle by 40% vs. uglifyjs.

That is nice stuff! So why did not the Closure Compiler take off?

The Issues with Minification as a Design Objective

The externs file was essential to right minification. With out it, CC would have mangled the fetchData perform title and the foo and bar properties, too, which might have resulted in runtime errors. Omitting a logo from an externs file would end in incorrect runtime habits that might be extraordinarily troublesome to trace down. In different phrases, this was a extremely dangerous developer expertise (DX).

CC launched some extralinguistic conventions to cope with this. For instance, in JS (and TS) there is not any distinction between utilizing dot notation and sq. braces to entry a property on an object:

const a =;
const b = obj['property'];
console.log(a, b);

This isn’t true with the Closure Compiler. Its conference is that quoted property entry is preserved whereas dotted will be mangled. This is how that code comes via the minifier with superior optimizations:


Word how the property names have diverged. In different phrases, whereas Closurized JavaScript is simply JavaScript, it additionally form of is not.

There’s one other large drawback with superior optimizations: in an effort to persistently mangle a property title, CC must have entry to all of the supply code that may use it. For this to be maximally efficient, all of the code you import should even be written with the Closure Compiler in thoughts, as should all of the code that that code imports, and so on.

Within the context of npm in 2023, this might be not possible. In most initiatives, a minimum of 90+% of the strains of code are third-party. For this fashion of minification to be efficient, all of that code must be written with the Closure Compiler in thoughts and compiled by it as a unit.

However at Google in 2004, or 2012, or maybe even as we speak, that is fairly sensible. At big corporations, the first- to third-party code ratio tends to be flipped. Utilizing third-party code is extra painful as a result of there are authorized and safety issues that include it, in addition to a lack of management. TypeScript’s zero runtime dependencies are a very good instance of this.

All of Google’s JavaScript was written with the Closure Compiler in thoughts and the overwhelming majority of it’s first-party. So superior optimizations works fantastically. However the remainder of the JS world does not function that approach. As quickly as you pull in any dependencies like React or Lodash that are not written with Closure Compiler in thoughts, it begins to lose its worth.

Distinction this with TypeScript. It solely must know concerning the sorts of current libraries. That is all that is wanted for kind checking. The DefinitelyTyped mission has been a monumental endeavor nevertheless it does imply that, usually talking, you may get TypeScript sorts for nearly any JS library. (There is a related, although a lot smaller, set of externs to get kind checking for standard JS libraries for the Closure Compiler.)

See Also

Stating it extra straight: superior optimizations requires that the compiler perceive a library’s implementation, not simply its sorts, and that is merely infeasible given the big variety of the JavaScript ecosystem.

Timing Is Every part

Google developed Closure c. 2004 nevertheless it wasn’t open sourced till late 2009. An O’Reilly e-book on it, Closure: The Definitive Guide, got here out in 2010.

Looking back this timing was horrible. In 2010, JavaScript was simply getting into its interval of most churn. JavaScript: The Good Parts got here out in 2008 and ES5 codified a lot of its suggestions in a brand new “strict” mode in 2009. Node.js was first launched in 2009 and npm adopted sizzling on its heels in 2010, creating the ecosystem of JavaScript packages we all know as we speak. npm grew considerably extra highly effective and helpful when browserify made it relevant to client-side code beginning in 2011.

And at last, CoffeeScript was launched in 2010. It normalized the concept of compiling an “improved” JavaScript right down to common JavaScript, as properly having a construct step. All of those influenced the route of JavaScript, with ES2015 bringing a number of the finest components of CoffeeScript into the language itself.

The Closure Compiler was developed within the period when JavaScript was a “dangerous” language that was to be prevented. CC itself is carried out in Java, which made it tougher to combine into an all-JS toolchain. And it tried so as to add lacking components to JavaScript. Because it could not add new syntax, it used particular capabilities: goog.present and goog.require offered a module system and goog.inherits smoothed out the process of making class hierarchies. These have been actual JavaScript capabilities that did one thing at runtime. If reminiscence serves, goog.require may inject a <script> tag!

There have been a couple of issues with this. One was that each one the goog capabilities strengthened the concept this was a device primarily constructed for Google. Placing firm names in your packages is frequent in Java, so presumably it felt pure for the Closure builders. Nevertheless it’s not in JavaScript. We simply import 'react', not “fb/react”.

Second, it made it awkward when JavaScript itself gained a module system and class key phrase. TypeScript confronted a few of these issues in its early days, too. It used to have its personal module system and sophistication system, however within the pursuits of ecosystem coherence it deprecated them in favor of the native options. TypeScript now lets JavaScript be JavaScript and innovates solely within the kind system.

This transition occurred early in TypeScript’s historical past, however late within the Closure Compiler’s. Presumably adaptation was tougher.

Why TypeScript received

TypeScript got here alongside at a greater time and has been in a position to adapt to the modifications in JavaScript and its ecosystem over the previous decade. It is self-hosted (tsc is written in TypeScript) and distributed with npm.

TypeScript additionally received by focusing extra on developer tooling. The Closure Compiler is an offline system: you run a command, it checks your program for errors, then you definitely edit and repeat. I am not conscious of any commonplace Closure language service. There is no equal of inspecting a logo in your editor to see what CC thinks its kind is. TypeScript, then again, locations as a lot emphasis on tsserver as tsc. Particularly with Visible Studio Code, which is written in TypeScript and got here out in 2015, TypeScript is a pleasure to make use of. TypeScript makes use of sorts to make you extra productive whereas Closure used them to level out your errors. No surprise builders most well-liked TypeScript!

(Google engineers aren’t any exception to this. Up to now decade they’ve adopted TypeScript and migrated to it en masse. You’ll be able to examine one workforce’s expertise porting Chrome Devtools from Closure to TypeScript).

TypeScript did a greater job of partaking the JavaScript group. TypeScript is developed and deliberate within the open on GitHub. They reply to bug experiences from anybody and deal with non-Microsoft customers as vital clients. The Closure Instruments, then again, have been very a lot an open supply launch of an inner Google device. Google was all the time the first client and exterior customers have been totally on their very own. The goog namespacing strengthened this.

Closure’s thought of “it is simply JavaScript” was interesting as a result of it allow you to keep away from a construct step. This stays interesting in 2023: some TypeScript customers nonetheless choose to make use of JSDoc-style kind annotations and --checkJs. However utilizing JSDoc for all sorts is awkward and noisy. Ergonomics do matter and TypeScript’s are undeniably higher.

Lastly, TypeScript’s central thought of “JavaScript + Sorts” has held up higher than the Closure Instruments’ thought of “minification” and “it is simply JavaScript”. Whereas shaving bytes off your bundle was all the trend in 2008, our connections are a lot quicker now and, whereas bundle dimension nonetheless issues, it’s not as essential because it was again then. Closure pressured a uniform system on you and all of your dependencies in an effort to obtain excessive minification. We have given up that aim in alternate for extra flexibility.

There is a common precept right here. I am reminded of Michael Feathers’s 2009 weblog publish 10 Papers Every Developer Should Read at Least Twice which discusses D.L. Parnas’s traditional 1972 paper “On the factors for use in decomposing techniques into modules”:

One other factor I actually like within the paper is his touch upon the KWIC system which he used for instance. He talked about that it will take a very good programmer every week or two to code. Right this moment, it will take virtually no time in any respect. Thumbs up for improved expertise and higher instruments. We’ve made progress.

The KWIC system principally types a textual content file. So are we right to laud our progress as software program builders? This is able to be a one-liner as we speak:

.break up('n')
.toSorted((a, b) => a.localeCompare(b))
.be a part of('n')

However take into consideration what makes this attainable:

  • We’re assuming that your entire file suits in reminiscence, which just about actually wouldn’t have been true in 1972.
  • We’re utilizing a rubbish collected language, which might have been a rarity again then.
  • We’ve an unlimited library at our fingertips through node built-ins and npm.
  • We’ve nice textual content editors and working techniques.
  • We’ve the online and StackOverflow: no must seek the advice of a reference handbook!

All of this stuff are due to advances in {hardware}. The {hardware} folks give us further transistors and the software program folks take most of these for ourselves to get a nicer improvement course of. So it’s with quicker community speeds and the Closure Compiler. We have taken again a few of that bandwidth in alternate for a extra versatile improvement course of and ecosystem.


There have been discussions of adding minification to TypeScript within the early days however now optimized output is an specific non-goal for the language. Should you’ve ever thought that type-driven minification can be a ravishing factor, the Closure Compiler is an interesting knowledge level. It may be tremendously efficient, nevertheless it additionally comes at an unlimited price to the ecosystem.

The Closure Compiler as a standalone exterior device appears principally useless (the closure playground is badly damaged and says “Copyright 2009”!). Nevertheless it nonetheless lives on at Google. Since they’ve adopted TypeScript, they’ll use the Closure Compiler for simply what it does finest: minification. To make this work, Google has constructed a device, tsickle, that makes TypeScript produce Closurized JavaScript. True to type, this device is open supply however fairly inscrutable to an outsider. It could be utilized by Angular however I could not inform.

Hopefully this was an attention-grabbing lesson in JavaScript historical past! The Closure Compiler represents another path that the JavaScript ecosystem might have taken, with totally different rules and totally different tradeoffs.

Source Link

What's Your Reaction?
In Love
Not Sure
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top