Name mangling in SlimIt 0.5

by Ruslan Spivak on June 6, 2011

SlimIt 0.5 is out. This release includes name mangling that can be turned on with -m or –mangle flag from the command line.

To give you a taste of what it’s like here is a simple example:

function test() {
  var long_name = 'long name', log = 5;
  console.log(long_name);
  foo = function(arg1, arg2) {
    var arg2 = 'new arg2';
    console.log(long_name + arg1 + arg2);
  };
}

After name mangling:

function a() {
  var a = 'long name', b = 5;
  console.log(a);
  foo = function(b, c) {
    var c = 'new arg2';
    console.log(a + b + c);
  };
}

And here is a new jQuery 1.6.1 minification benchmark for jsmin, rJSmin, and SlimIt (the lower the better):

jQuery 1.6.1 (bytes) jsmin rJSmin SlimIt
234,995 134,819 134,215 94,290

Please, file any bug reports at GitHub.

If you enjoyed this post why not subscribe via email or my RSS feed and get the latest updates immediately. You can also follow me on GitHub or Twitter.

{ 9 comments… read them below or add one }

Peter Ward June 6, 2011 at 1:46 AM

I have no idea if this is actually worth the extra effort, but with name mangling, you could use huffman coding to give the more frequently used symbols shorter names. It would only have an effect when you start having symbols with >= 2 characters in their mangled names.

Reply

Ruslan Spivak June 6, 2011 at 7:55 AM

Hi Peter,

Thanks for the suggestion.

Reply

Dj Gilcrease June 6, 2011 at 4:59 AM

There is an error in your mangled example, it make the a function un-recursable

Reply

Ruslan Spivak June 6, 2011 at 7:49 AM

If you add an actual recursive function call the name mangling should work as expected:

[sourcecode language="javascript"]
function test() {
var long_name = ‘long name’, log = 5;
console.log(long_name);
test();
}
[/sourcecode]

After mangling:
[sourcecode language="javascript"]
function a() {
var b = ‘long name’, c = 5;
console.log(b);
a();
}
[/sourcecode]

Reply

Peter Ward June 6, 2011 at 7:53 AM

@DJ Gilcrease:
Looks fine to me… http://paste.pocoo.org/show/401566/

Reply

Laurence Rowe June 6, 2011 at 9:17 AM

As you were already beating the other Python based minifiers so resoundingly, I thought I’d take a look at some other minifiers.

The official jQuery release uses the UglifyJS minifier according to https://github.com/jquery/jquery/tree/master/build/lib. The official minified release is 90,975 bytes (with the copyright header removed).

With http://marijnhaverbeke.nl/uglifyjs I get 90,985 bytes.

With http://closure-compiler.appspot.com/home I get 90,557 bytes.

Reply

Ruslan Spivak June 6, 2011 at 10:20 AM

Hi Laurence,

Those are tough competitors :)

Reply

Alex Buck June 8, 2011 at 4:44 PM

Assuming foo name was not mangled becuase it is in the global scope it may be nice to have a –warn flag for globals similar to jslint. Good work though dood.

Reply

Ruslan Spivak June 10, 2011 at 12:41 AM

Hey Alex,

Thanks for the idea. You can also add it to issues so that I don’t forget :)

Reply

Speak your mind

Previous post:

Next post: