Skip to content
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

Fixed rare issue with components initialization #292

Merged
merged 2 commits into from
Aug 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
objEach
} from './utils';

var {Node} = window;
var elProto = window.HTMLElement.prototype;
var nativeMatchesSelector = (
elProto.matches ||
Expand Down Expand Up @@ -367,12 +368,14 @@ function triggerLifecycle (target, component) {
* @returns {undefined}
*/
function initElements (elements) {
var elementsLen = elements.length;

for (var a = 0; a < elementsLen; a++) {
// [CATION] Don't cache elements length! Components initialization could append nodes
// as siblings (see label's element behaviour for example) and this could lead to problems with
// components placed at the end of processing childNodes because they will change they index
// position and get out of cached value range.
for (var a = 0; a < elements.length; a++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only gripe here is that it's cheaper in IE9 to save elements.length as elementsLen in super massive doms.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I guess we can't do that :(. I wonder what the performance would be like now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is super massive DOM trees are real world scenario or just synthetic test case?

var element = elements[a];

if (element.nodeType !== 1 || element.attributes[ATTR_IGNORE]) {
if (element.nodeType !== Node.ELEMENT_NODE || element.attributes[ATTR_IGNORE]) {
continue;
}

Expand Down Expand Up @@ -403,12 +406,11 @@ function initElements (elements) {
* @returns {undefined}
*/
function removeElements (elements) {
var len = elements.length;

for (var a = 0; a < len; a++) {
// Don't cache `childNodes` length. For more info see description in `initElements` function.
for (var a = 0; a < elements.length; a++) {
var element = elements[a];

if (element.nodeType !== 1) {
if (element.nodeType !== Node.ELEMENT_NODE) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/mutation-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
objEach
} from './utils';

var Attr = window.Attr;
var {Node, Attr} = window;
var NativeMutationObserver = window.MutationObserver || window.WebkitMutationObserver || window.MozMutationObserver;
var isFixingIe = false;
var isIe = window.navigator.userAgent.indexOf('Trident') > -1;
Expand Down Expand Up @@ -158,7 +158,7 @@ MutationObserver.prototype = {
//
// IE 11 bug: https://connect.microsoft.com/IE/feedback/details/817132/ie-11-childnodes-are-missing-from-mutationobserver-mutations-removednodes-after-setting-innerhtml
var shouldWorkAroundIeRemoveBug = isFixingIe && eType === 'DOMNodeRemoved';
var isDescendant = lastBatchedElement && lastBatchedElement.nodeType === 1 && elementContains(lastBatchedElement, eTarget);
var isDescendant = lastBatchedElement && lastBatchedElement.nodeType === Node.ELEMENT_NODE && elementContains(lastBatchedElement, eTarget);

// This checks to see if the element is contained in the last batched
// element. If it is, then we don't batch it because elements are
Expand Down