<script>
const isObject = (ref)      => typeof ref=='object' && ref!=null;

var app = {
	form: {
		attributes: [
			{sum:15, allowed:1},
			{sum:15, allowed:2},
			{sum:15, allowed:3}
			]
		},

	mix: function()
		{
		var i, i_end;
		i_end = app.form.attributes.length;
		for (i=0; i<i_end; i++)
			{
	                app.form.attributes[i].sum = Math.floor(Math.random()*i_end*10);
			}
		},

        RecomputeAttributePriorities: function() { 
            var AttrAllowed = [7,6,5]
            var AttrSums = [
                app.form.attributes[0].sum,
                app.form.attributes[1].sum,
                app.form.attributes[2].sum
            ]            
            AttrSums.sort(function(a,b) {return a-b}).reverse()
            var changed = []
            while (AttrSums.length > 0) {
                for (var i=0; i < app.form.attributes.length; i++) {
                    if (app.form.attributes[i].sum == AttrSums[0] && !(changed.includes(i))) {
                        app.form.attributes[i].allowed = AttrAllowed[0]
                        AttrSums.shift()
                        AttrAllowed.shift()
                        changed.push(i)
                    }
                }
            }
	},

        computeAttrPriorities: function() {
            var i, j, tmp=[], group = app.form.attributes, i_end=group.length;
            for (i=0; i<i_end; i++)
                {
                tmp[i] = [group[i].sum, i];
                }
            tmp.sort( function(b, a) {return a[0] - b[0];} );
            for (i=0; i<i_end; i++)
                {
                group[tmp[i][1]].allowed = i_end + 4 - i; //'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.charAt(i);
                }
        },



}

var i, m=100000, t0, t1;

t0 = performance.now();
for (i=0; i<m; i++) {app.mix(); app.RecomputeAttributePriorities()}
t1 = performance.now();
console.log(`AAA ${t1 - t0} milliseconds. ${JSON.stringify(app.form.attributes)}`);

t0 = performance.now();
for (i=0; i<m; i++) {app.mix(); app.computeAttrPriorities()}
t1 = performance.now();
console.log(`BBB ${t1 - t0} milliseconds. ${JSON.stringify(app.form.attributes)}`);

t0 = performance.now();
for (i=0; i<m; i++) {app.mix(); app.RecomputeAttributePriorities()}
t1 = performance.now();
console.log(`AAA ${t1 - t0} milliseconds. ${JSON.stringify(app.form.attributes)}`);

t0 = performance.now();
for (i=0; i<m; i++) {app.mix(); app.computeAttrPriorities()}
t1 = performance.now();
console.log(`BBB ${t1 - t0} milliseconds. ${JSON.stringify(app.form.attributes)}`);

t0 = performance.now();
for (i=0; i<m; i++) {app.mix(); app.RecomputeAttributePriorities()}
t1 = performance.now();
console.log(`AAA ${t1 - t0} milliseconds. ${JSON.stringify(app.form.attributes)}`);

t0 = performance.now();
for (i=0; i<m; i++) {app.mix(); app.computeAttrPriorities()}
t1 = performance.now();
console.log(`BBB ${t1 - t0} milliseconds. ${JSON.stringify(app.form.attributes)}`);


</script>