Randomize variable names each time UglifyJS runs

By | February 12, 2019 | 0 Comment
Category: Javascript Tags: ,

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. It is extremely easy for someone to beautify your previous code and updated code and use diff tools to compare the change. It very quickly gives a pin point where the code was changed. It defeats the purpose.

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 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;

This code adds a random number between 0 and 50 which will work as a seed. You may adjust this seed based on your need.

About Vishal Monpara

Vishal Monpara is a full stack Solution Developer/Architect with 13 years of experience primarily using Microsoft stack. He is currently working in Retail industry and moving 1's and 0's from geographically dispersed hard disks to geographically dispersed user leveraging geographically dispersed team members.