Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Chrome 130
Mac OS X 10.15.7
Desktop
2 months ago
Test name Executions per second
disconnect/reconnect with 1 observe 1190947.1 Ops/sec
disconnect/reconnect with 2 observes 973994.2 Ops/sec
HTML Preparation code:
AخA
 
1
<div id="test">Test</div>
Script Preparation code:
x
 
const __OBSERVER = Symbol('observer');
const div = document.getElementById('test');
function update1() {
    if(!div[__OBSERVER]) {
        div[__OBSERVER] = new MutationObserver(mutations => update1());
    } else {
        div[__OBSERVER].disconnect();
    }
    div.style.opacity = parseInt(div.style.opacity) ? 0 : 1;
    div[__OBSERVER].observe(div, { attributeFilter: ['style'] });
}
function update2() {
    if(!div[__OBSERVER]) {
        div[__OBSERVER] = new MutationObserver(mutations => update2());
    } else {
        div[__OBSERVER].disconnect();
    }
    div.style.opacity = parseInt(div.style.opacity) ? 0 : 1;
    div[__OBSERVER].observe(div, { attributeFilter: ['style'] });
    div[__OBSERVER].observe(div, { characterData: true, subtree: true });
}
Tests:
  • disconnect/reconnect with 1 observe

     
    update1();
  • disconnect/reconnect with 2 observes

     
    update2();