-
Notifications
You must be signed in to change notification settings - Fork 51k
Perf: Insert nodes top-down in IE and Edge #5253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /** | ||
| * Copyright 2015, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. An additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| * | ||
| * @providesModule DOMLazyTree | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| /** | ||
| * In IE (8-11) and Edge, appending nodes with no children is dramatically | ||
| * faster than appending a full subtree, so we essentially queue up the | ||
| * .appendChild calls here and apply them so each node is added to its parent | ||
| * before any children are added. | ||
| * | ||
| * In other browsers, doing so is slower or neutral compared to the other order | ||
| * (in Firefox, twice as slow) so we only do this inversion in IE. | ||
| * | ||
| * See https://github.com/spicyj/innerhtml-vs-createelement-vs-clonenode. | ||
| */ | ||
| var enableLazy = ( | ||
| typeof document !== 'undefined' && | ||
| typeof document.documentMode === 'number' | ||
| || | ||
| typeof navigator !== 'undefined' && | ||
| typeof navigator.userAgent === 'string' && | ||
| /\bEdge\/\d/.test(navigator.userAgent) | ||
| ); | ||
|
|
||
| function insertTreeChildren(tree) { | ||
| if (!enableLazy) { | ||
| return; | ||
| } | ||
| var node = tree.node; | ||
| var children = tree.children; | ||
| if (children.length) { | ||
| for (var i = 0; i < children.length; i++) { | ||
| insertTreeBefore(node, children[i], null); | ||
| } | ||
| } else if (tree.html != null) { | ||
| node.innerHTML = tree.html; | ||
| } | ||
| } | ||
|
|
||
| function insertTreeBefore(parentNode, tree, referenceNode) { | ||
| parentNode.insertBefore(tree.node, referenceNode); | ||
| insertTreeChildren(tree); | ||
| } | ||
|
|
||
| function replaceChildWithTree(oldNode, newTree) { | ||
| oldNode.parentNode.replaceChild(newTree.node, oldNode); | ||
| insertTreeChildren(newTree); | ||
| } | ||
|
|
||
| function queueChild(parentTree, childTree) { | ||
| if (enableLazy) { | ||
| parentTree.children.push(childTree); | ||
| } else { | ||
| parentTree.node.appendChild(childTree.node); | ||
| } | ||
| } | ||
|
|
||
| function queueHTML(tree, html) { | ||
| if (enableLazy) { | ||
| tree.html = html; | ||
| } else { | ||
| tree.node.innerHTML = html; | ||
| } | ||
| } | ||
|
|
||
| function DOMLazyTree(node) { | ||
| return { | ||
| node: node, | ||
| children: [], | ||
| html: null, | ||
| }; | ||
| } | ||
|
|
||
| DOMLazyTree.insertTreeBefore = insertTreeBefore; | ||
| DOMLazyTree.replaceChildWithTree = replaceChildWithTree; | ||
| DOMLazyTree.queueChild = queueChild; | ||
| DOMLazyTree.queueHTML = queueHTML; | ||
|
|
||
| module.exports = DOMLazyTree; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@patrickkettner @csuwildcat Here in React, I added a user-agent test that catches all versions of Edge (and IE) because the DOM implementation in Edge is much slower when reparenting large trees than other browsers. That is, to create the structure
the operations
are most natural for us, but we are forced to reorder the operations in Edge to be
in order to get reasonable performance. See the link in the comment above for more details. This has not shipped in a React release yet. I heard that there is a chance this may be fixed soon in Edge – obviously I would prefer to not ship a React with this feature test and would much prefer to put an upper bound on the versions of Edge where we use this workaround. Anything I can do to help get this fixed in Edge soon?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is more of a curiosity than anything: does the perf profile look the same if you do the same sequence of appends with nodes in a Document Fragment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not certain but it didn't seem likely to make a difference. That is,
parent.appendChild(newChild)seems to take time proportional to the number of nodes in thenewChildsubtree, whereas every other browser seems to make it constant time.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that too back then, made no difference that I could tell.