Skip to content

Instantly share code, notes, and snippets.

@rniwa
Last active August 7, 2020 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rniwa/43508508f1d418f9f576 to your computer and use it in GitHub Desktop.
Save rniwa/43508508f1d418f9f576 to your computer and use it in GitHub Desktop.
why-is-dom-transaction-hard
mutateElementToAffectOffsetTop(someElement);

// (1) We don't know the value of someElement.offsetTop here because we compute it lazily.

var y = (new DOMTransaction(function () {
    // (2) We need to remember the "old value" of someElement.offsetTop here.
    mutateElementToAffectOffsetTop(someElement);
    return someElement.offsetTop; 
})).execute();

If DOMTransaction were to preserve the old value, then y needs to be identically equal to the value of someElement.offsetTop at (1). However, since nobody had asked for the value in (1), we're forced to pre-compute the value at (2). It's worse than that. mutateElementToAffectOffsetTop might be using the old value of other computed CSS values in which case we have to pre-compute all the property values that could be queried by the script at (2).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment