Synchronizing two scrollbars to work in unison This synchronizes the document’s vertical scrollbar and a DIV’s horizontal scrollbar so they can work in unison controlling the content simultaneously.

The code is operable and behaves how I want. I want to know whether or not this code can be optimized any further.

Heads-up: the jsScroll0 and jsScroll1 flags stop either scrollbar from locking due to an infinite loop scenario upon each scrollbar trying to set each other’s values.

(function() {
    var jsScroll0, jsScroll1;
    var divScroller = document.getElementById('scrollP');

    document.onscroll = function () {
        if (jsScroll1) {
            jsScroll1 = false;
            return;
        }
        jsScroll0 = true;
        divScroller.scrollLeft = document.documentElement.scrollTop;
    };
    divScroller.onscroll = function () {
        if (jsScroll0) {
            jsScroll0 = false;
            return;
        }
        jsScroll1 = true;
        document.documentElement.scrollTop = divScroller.scrollLeft;
    };
})();
This looks perfectly good to me.  Everything has a unique purpose, everything is either clearly explained or self-explanatory.  Unless you have tested this and found a performance issue, and you are sure that it is happening here, do not do any optimization to this.  

There is no loop and no recursion, the code itself can't properly be an optimization target.  If you are observing that this is running slowly, the hidden costs of setting the scrollbars must be the cause.  To address this you will need to change your entire strategy to keeping the div contents in the right place using CSS only.  I have to say that while I find using the position of some element (scrollP?) to be a pretty elegant way to duck issues of messing around with coordinates that will be inconsistent otherwise, it is technically the best practice to do all positioning with CSS.