Behavior changing when blank console.log() call inserted

Viewed 134

Code

var repro_bug = () => {
    var state = { x: 1, y: 1 };

    return () => {
        var toggle = () => {
            state.y = 1;

            console.log(state.x);
        };

        // console.log();

        state.x = state.y;
        state.y = 0;

        toggle();
    };
};

var run = repro_bug();

Bug

If you call run(), it should always log 1. On Chrome 91.0.4472.114 (on Chrome OS 91.13904.0), when I call run() ten times or so, it starts logging 0 instead.

Oddly, if you uncomment line 11, a blank console.log, it works as expected. This leads me to believe it's a bug in Chrome, potentially involving some sort of out-of-order execution. This example is the shortest I could get it, the separate toggle function and object for state (instead of two variables) are both necessary for it to behave oddly.

Note that you need to do the final run() manually, as shown in the screenshots below, in order to reproduce. This is likely timing related in some way.

Cannot reproduce in Node, and another person can't reproduce on Firefox.

Screenshots

Uncommented (expected behavior):

Uncommented: Logs 1 on 1st-11th runs

Commented:

Commented: Logs 1 on 1st-10th runs and 0 on 11th run

0 Answers
Related