Extreme JavaScript Minification and Obfuscation

3,513 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,513
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Extreme JavaScript Minification and Obfuscation

  1. 1. Extreme (yet non-intelligent) Minification and Obfuscation of JavaScript
  2. 2. 1. Comments 1) strip // comments 2) strip /* comments */ 359Kb -> 290Kb(-69Kb)
  3. 3. 2. White spaces . 3) strip white spaces 359Kb -> 290Kb(-69Kb)->239Kb(-51Kb)
  4. 4. 3. Variable names var bVariable , hVariable ... oVariable ; (a=Array, b=Boolean, c=Class, etc. ) 4) find all names of variables 5) sort list on amount of occurances 6) replace with {a..z}, {a...z}{a...z} 359Kb -> 290Kb(-69Kb)->239Kb(-51Kb)->138Kb(-101Kb)
  5. 5. 4. Object Members and String Values oObject .member = function() {...}; var sValue = "stringvalue" ; 7) find all members and strings 8) sort list on amount of occurences 9) put unique values into " _ " JavaScript array 10) replace oObject .member with oObject [_[N]] 11) replace "stringvalue" with _[N] 359Kb -> 290Kb(-69Kb)->239Kb(-51Kb)->138Kb(-101Kb)->128Kb(-10Kb)
  6. 6. 5. JavaScript Keywords/Operators/Constants function fTest ( bValue, sTest ) { for ( var nIndex = 0; nIndex < 10; nIndex ++ ) if ( bValue == true || sTest == &quot;test&quot; ) break ; } 12) create dictionary with keywords 13) encode keywords 13) replace keywords in code with decoded ones 14) wrap code with decoder 15) in decoder restore dictionary and code 16) evaluate code with Function constructor 359Kb -> 290Kb(-69Kb)->239Kb(-51Kb)->138Kb(-101Kb)->128Kb(-10Kb)->111Kb(-17Kb)
  7. 7. Appendix A: Compiler Comparison Original: 359Kb Ample SDK Compiler: Ample Mode: 111Kb (69% compression) YUI Compressor 2.4.2: Normal Mode: 189Kb (34%/47% compression) Google Closure Compiler: Simple Mode: 126Kb (65% compression) Advanced Mode: 93Kb (74% compression!) Comment: code doesn't run
  8. 8. Appendix B: No title Original code without comments and spaces zip: 239Kb->46Kb Obfuscated/minified code zip: 111Kb->39Kb - 7Kb saved! - Code unreadable and fun!
  9. 9. Thanks! Ample SDK http://www.amplesdk.com me, Sergey ilinsky http://www.ilinsky.com http://twitter.com/ilinsky

×