Skip to content

Commit

Permalink
perf(render): precompute # bound text nodes and root nodes in `DomPro…
Browse files Browse the repository at this point in the history
…toView`
  • Loading branch information
tbosch committed Jun 11, 2015
1 parent 9cd510a commit 24e647e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion modules/angular2/src/dom/html_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Html5LibDomAdapter implements DomAdapter {
throw 'not implemented';
}
content(node) {
throw 'not implemented';
return node;
}

firstChild(el) => el is NodeList ? el.first : el.firstChild;
Expand Down
15 changes: 7 additions & 8 deletions modules/angular2/src/render/dom/dom_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,11 @@ export class DomRenderer extends Renderer {
rootElementClone = DOM.importIntoDoc(DOM.content(protoView.element));
elementsWithBindingsDynamic =
DOM.querySelectorAll(rootElementClone, NG_BINDING_CLASS_SELECTOR);
var childNode = DOM.firstChild(rootElementClone);
// TODO(perf): Should be fixed size, since we could pre-compute in in DomProtoView
viewRootNodes = [];
viewRootNodes = ListWrapper.createFixedSize(protoView.rootNodeCount);
// Note: An explicit loop is the fastest way to convert a DOM array into a JS array!
while (childNode != null) {
ListWrapper.push(viewRootNodes, childNode);
childNode = DOM.nextSibling(childNode);
var childNode = DOM.firstChild(rootElementClone);
for (var i = 0; i < protoView.rootNodeCount; i++, childNode = DOM.nextSibling(childNode)) {
viewRootNodes[i] = childNode;
}
} else {
rootElementClone = DOM.importIntoDoc(protoView.element);
Expand All @@ -239,8 +237,9 @@ export class DomRenderer extends Renderer {
}

var binders = protoView.elementBinders;
var boundTextNodes = [];
var boundTextNodes = ListWrapper.createFixedSize(protoView.boundTextNodeCount);
var boundElements = ListWrapper.createFixedSize(binders.length);
var boundTextNodeIdx = 0;

for (var binderIdx = 0; binderIdx < binders.length; binderIdx++) {
var binder = binders[binderIdx];
Expand All @@ -261,7 +260,7 @@ export class DomRenderer extends Renderer {
// boundTextNodes
var textNodeIndices = binder.textNodeIndices;
for (var i = 0; i < textNodeIndices.length; i++) {
ListWrapper.push(boundTextNodes, childNodes[textNodeIndices[i]]);
boundTextNodes[boundTextNodeIdx++] = childNodes[textNodeIndices[i]];
}

// contentTags
Expand Down
10 changes: 9 additions & 1 deletion modules/angular2/src/render/dom/view/proto_view.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {isPresent} from 'angular2/src/facade/lang';
import {DOM} from 'angular2/src/dom/dom_adapter';

import {List} from 'angular2/src/facade/collection';
import {List, ListWrapper} from 'angular2/src/facade/collection';

import {ElementBinder} from './element_binder';
import {NG_BINDING_CLASS} from '../util';
Expand All @@ -27,6 +27,8 @@ export class DomProtoView {
rootBindingOffset: number;
// the number of content tags seen in this or any child proto view.
transitiveContentTagCount: number;
boundTextNodeCount: number;
rootNodeCount: number;

constructor({elementBinders, element, transitiveContentTagCount}) {
this.element = element;
Expand All @@ -35,5 +37,11 @@ export class DomProtoView {
this.isTemplateElement = DOM.isTemplateElement(this.element);
this.rootBindingOffset =
(isPresent(this.element) && DOM.hasClass(this.element, NG_BINDING_CLASS)) ? 1 : 0;
this.boundTextNodeCount =
ListWrapper.reduce(elementBinders, (prevCount: number, elementBinder: ElementBinder) =>
prevCount + elementBinder.textNodeIndices.length,
0);
this.rootNodeCount =
this.isTemplateElement ? DOM.childNodes(DOM.content(this.element)).length : 1;
}
}

0 comments on commit 24e647e

Please sign in to comment.