Randomize Variable Names Each Time UglifyJS Runs

Category: JavascriptTags: ,

Why Randomize Variable Names?

If you are using UglifyJS to uglify your JavaScript code, you are using it only to serve a single purpose – to make it hard for anyone to peek into your code. But if you have minor changes to the code, variable names still come out pretty much same. So it is extremely easy for someone to beautify your code and use diff tools to compare the change and defeats the purpose.

How to Randomize Variable Names?

Here is a quick way to randomize the variable names each time you run UglifyJS. This way when you beautify and diff the two versions, you will see diffs almost everywhere and makes little more hard to pin point the changed code.

I assume you are using UglifyJS as a node module. Go to UglifyJS node module location > lib > scope.js file. At the bottom, you would see a definition of function base54. Replace the whole definition with the following code and you are all set.

var base54 = (function() {
    var freq = Object.create(null);
    var max = 50, min = 0;
    var rnd = Math.floor(Math.random()*(max-min+1)+min);
    function init(chars) {
        var array = [];
        for (var i = 0, len = chars.length; i < len; i++) {
            var ch = chars[i];
            freq[ch] = -1e-2 * i;
        return array;
    var digits = init("0123456789");
    var leading = init("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_");
    var chars, frequency;
    function reset() {
        frequency = Object.create(freq);
    base54.consider = function(str, delta) {
        for (var i = str.length; --i >= 0;) {
            frequency[str[i]] += delta;
    function compare(a, b) {
        return frequency[b] - frequency[a];
    base54.sort = function() {
        chars = leading.sort(compare).concat(digits.sort(compare));
    base54.reset = reset;
    function base54(num) {
        var ret = "", base = 54;
		num = num + rnd;
        do {
            ret += chars[num % base];
            num = Math.floor(num / base);
            base = 64;
        } while (num > 0);
        return ret;
    return base54;

How this Code Works?

Above code shows highlighted line. Only these lines are changed within the function. It adds a random number between 0 and 50 which will work as a seed. You may adjust this seed based on your need.

UglifyJS Version

Above script is tested with following UglifyJS versions.

  • 3.5 – February, 2019
  • 3.9.4 – June, 2020
  • 3.11.6 – November, 2020



Your email address will not be published. Required fields are marked *