Uglifyjs: different optimization by running Uglify twice?

Created on 31 May 2018  路  5Comments  路  Source: mishoo/UglifyJS

source code:

function calculateFingerprint(code) {
    var pow = calculatePow(kGram - 1);
    var hashs = [];
    var hash = calculateHash(code.substring(0, kGram));
    hashs.push([hash, 0]);
    for (var i = kGram; i < code.length; i++) {
        hash = (base * (hash - pow * code.charCodeAt(i - kGram)) + code.charCodeAt(i)) % prime;
        if (hash < 0) {
            hash += prime;
        }
        hashs.push([hash, i - kGram + 1]);
    }
    var minHash = hash;
    var fingerprint = {};
    var count = hashs.length - windowSize + 1;
    if (count <= 0) {
        count = 1;
    }
    for (var i = 0; i < count; i++) {
        minHash = hashs[i][0];
        var position = hashs[i][1];
        for (var j = 1; j < windowSize; j++) {
            if (i + j < hashs.length && hashs[i + j][0] < minHash) {
                minHash = hashs[i + j][0];
                position = hashs[i + j][1];
            }
        }
        fingerprint[minHash] = position;
    }
    return fingerprint;
}

var UglifyJS = require("uglify-js");

var fs = require('fs');

var code = fs.readFileSync('./a.js', 'utf-8');

console.log(code);
console.log('------------------1-----------------')

var code = UglifyJS.minify(code).code;
console.log(code);
console.log('------------------2-----------------')

code = UglifyJS.minify(code).code;
console.log(code);
console.log('------------------3----------------')

code = UglifyJS.minify(code).code;
console.log(code);
console.log('------------------4----------------')

first print

function calculateFingerprint(r) {
    var a = calculatePow(kGram - 1),
        e = [],
        t = calculateHash(r.substring(0, kGram));
    e.push([t, 0]);
    for (var n = kGram; n < r.length; n++)(t = (base * (t - a * r.charCodeAt(n - kGram)) + r.charCodeAt(n)) % prime) < 0 && (t += prime), e.push([t, n - kGram + 1]);
    var i = t,
        c = {},
        l = e.length - windowSize + 1;
    l <= 0 && (l = 1);
    for (n = 0; n < l; n++) {
        i = e[n][0];
        for (var o = e[n][1], h = 1; h < windowSize; h++) n + h < e.length && e[n + h][0] < i && (i = e[n + h][0], o = e[n + h][1]);
        c[i] = o
    }
    return c
}

second print

function calculateFingerprint(r) {
    var a = calculatePow(kGram - 1),
        e = [],
        t = calculateHash(r.substring(0, kGram));
    e.push([t, 0]);
    for (var n = kGram; n < r.length; n++)(t = (base * (t - a * r.charCodeAt(n - kGram)) + r.charCodeAt(n)) % prime) < 0 && (t += prime), e.push([t, n - kGram + 1]);
    var i = t,
        c = {},
        l = e.length - windowSize + 1;
    for (l <= 0 && (l = 1), n = 0; n < l; n++) {
        i = e[n][0];
        for (var o = e[n][1], h = 1; h < windowSize; h++) n + h < e.length && e[n + h][0] < i && (i = e[n + h][0], o = e[n + h][1]);
        c[i] = o
    }
    return c
}

## third print same with the second.

Most helpful comment

@alexlamsl idempotent output should definitely be a requirement for a code minifier...

All 5 comments

Not a bug.

why is it so

Because of uglify changing output between runs, I got different file hashes. @iamaddy @alexlamsl is there any way to avoid this?

Any pinpoint on cause?

@alexlamsl idempotent output should definitely be a requirement for a code minifier...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gabmontes picture gabmontes  路  5Comments

JoeUX picture JoeUX  路  3Comments

hacdias picture hacdias  路  5Comments

kzc picture kzc  路  5Comments

PinkyJie picture PinkyJie  路  3Comments