我正在开发一个小型JavaScript模板引擎,当模型发生更改时,我有两种处理DOM更新的可能方法:
Check if the DOM update is really needed before doing it. This has the benefit of not risking unnecessary updates, but I am wasting space on keeping track of old values.
if (oldValue !== newValue) { element.textContent = newValue; }
Just do it. This is obviously simpler, but I am afraid that I will be triggering repaints and reflows for no reason.
element.textContent = newValue;
setAttribute
、addClass
和 removeClass
,以及设置 style[prop] = value
来操作DOM。因此,我的问题是:如果您在不实际更改任何内容的情况下触摸DOM,现代浏览器是否足够聪明,以便注意到没有实际更改,因此不运行回流或重绘?