msg() function in core.js thrashes style information in a loop

Issue #101 resolved
Boris Zbarsky created an issue

The function has this bit:

            dojo.forEach(dojo.query("*", gameLog), function (entry, i) {
                if (i > 25) {
                    var opacity = dojo.getStyle(entry, "opacity");
                    dojo.setStyle(entry, "opacity", opacity - 0.033);
                }
            });

this is an instance of the antipattern where style information is modified and then queried in a loop, forcing a recomputation at every query, which takes nontrivial time, because style information has been modified in the interim.

Why is the query needed at all? We know that the last time msg was called the opacity was set to 1 for all the things that are currently at i <= 26, and for i larger than that it decreases by 0.033 each time. So the desired opacity to set can just be directly computed from i as 1 - (i-25)*0.33, I would think.

Comments (4)

  1. freeroot

    You suggest something like that ?

    dojo.forEach(dojo.query("*", gameLog), function (entry, i) {
        if (i > 25) {
            dojo.setStyle(entry, "opacity", 1 - (i-25)*0.33);
        }
    });
    
  2. Log in to comment