Share on Facebook Tweet on Twitter Share on LinkedIn Share by email
JSZap: Compressing JavaScript Source Code

JavaScript is widely used in web-based applications, and gigabytes of JavaScript code are transmitted over the Internet every day. JSZap reduces the size of JavaScript source code by converting it into an abstract syntax tree (AST) and compressing the AST. As a result, fewer data need to be transmitted and less work has to be performed by the client after receiving the JavaScript AST.


JavaScript is widely used in web-based applications and gigabytes of JavaScript code are transmitted over the Internet every day. Current efforts to compress JavaScript to reduce network delays and server bandwidth requirements rely on syntactic changes to the source code and content encoding using gzip. In this paper, we consider reducing the JavaScript source code to a compressed abstract syntax tree (AST) and transmitting the code in this format. An AST-based representation has a number of benefits including reducing parsing time on the client, fast checking for well-formedness, and, as we show, compression.

With JSZap, we transform the JavaScript source into three streams: AST production rules, identifiers, and literals, each of which is compressed independently. While previous work has compressed Java programs using ASTs for network transmission, no prior work has applied and evaluated these techniques for JavaScript source code, despite the fact that it is by far the most commonly transmitted program representation. We show that in JavaScript the literals and identifiers constitute the majority of the total file size and we describe techniques that compress each stream effectively. On average, compared to gzip we reduce the production, identifier, and literal streams by 30%, 12%, and 4%, respectively. Overall, we reduce total file size by 10% compared to gzip while, at the same time, benefiting the client by moving some of the necessary processing to the server.



This figure shows the compression benefits of JSZap over the current standard JavaScript compression based on content-encoding using gzip.

Papers and presentations

"JSZap: Compressing JavaScript Code", Martin Burtscher, Benjamin Livshits, Gaurav Sinha, and Benjamin Zorn,  

Microsoft Research Technical Report MSR-TR-2010-21, March 2010

16 pages