Skip to content

Commit

Permalink
Avoid using .slice and .forEach
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Orvell committed Nov 3, 2015
1 parent 53f3a7d commit d2c02a9
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 59 deletions.
5 changes: 3 additions & 2 deletions src/lib/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@
*/
extend: function(prototype, api) {
if (prototype && api) {
Object.getOwnPropertyNames(api).forEach(function(n) {
var n$ = Object.getOwnPropertyNames(api);
for (var i=0, n; (i<n$.length) && (n=n$[i]); i++) {
this.copyOwnProperty(n, api, prototype);
}, this);
}
}
return prototype || api;
},
Expand Down
12 changes: 6 additions & 6 deletions src/lib/collection.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@
_applySplices: function(splices) {
// Dedupe added and removed keys to a final added/removed map
var keyMap = {}, key, i;
splices.forEach(function(s) {
for (var i=0, s; (i<splices.length) && (s=splices[i]); i++) {
s.addedKeys = [];
for (i=0; i<s.removed.length; i++) {
key = this.getKey(s.removed[i]);
for (j=0; j<s.removed.length; j++) {
key = this.getKey(s.removed[j]);
keyMap[key] = keyMap[key] ? null : -1;
}
for (i=0; i<s.addedCount; i++) {
var item = this.userArray[s.index + i];
for (j=0; j<s.addedCount; j++) {
var item = this.userArray[s.index + j];
key = this.getKey(item);
key = (key === undefined) ? this.add(item) : key;
keyMap[key] = keyMap[key] ? null : 1;
Expand All @@ -148,7 +148,7 @@
// further changes to the array by the time the splice is consumed
s.addedKeys.push(key);
}
}, this);
}
// Convert added/removed key map to added/removed arrays
var removed = [];
var added = [];
Expand Down
14 changes: 4 additions & 10 deletions src/lib/dom-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -791,15 +791,6 @@

DomApi.prototype._distributeParent = function() {};

var nativeForwards = ['appendChild', 'insertBefore',
'removeChild', 'replaceChild'];

nativeForwards.forEach(function(forward) {
DomApi.prototype[forward] = function() {
return this.node[forward].apply(this.node, arguments);
};
});

Object.defineProperties(DomApi.prototype, {

childNodes: {
Expand Down Expand Up @@ -998,7 +989,10 @@
hasInsertionPoint: hasInsertionPoint,
ctor: DomApi,
factory: factory,
hasDomApi: hasDomApi
hasDomApi: hasDomApi,
arrayCopy: arrayCopy,
arrayCopyChildNodes: arrayCopyChildNodes,
arrayCopyChildren: arrayCopyChildren
};

})();
Expand Down
5 changes: 3 additions & 2 deletions src/lib/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
var user = window.Polymer || {};

// via url
location.search.slice(1).split('&').forEach(function(o) {
var parts = location.search.slice(1).split('&');
for (var i=0, o; (i < parts.length) && (o=parts[i]); i++) {
o = o.split('=');
o[0] && (user[o[0]] = o[1] || true);
});
}

var wantShadow = (user.dom === 'shadow');
var hasShadow = Boolean(Element.prototype.createShadowRoot);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/style-util.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
var cssText = '';
// if element is a template, get content from its .content
var content = element.content || element;
var e$ = Array.prototype.slice.call(
var e$ = Polymer.DomApi.arrayCopy(
content.querySelectorAll(this.MODULE_STYLES_SELECTOR));
for (var i=0, e; i < e$.length; i++) {
e = e$[i];
Expand Down Expand Up @@ -145,7 +145,7 @@

resolveCss: Polymer.ResolveUrl.resolveCss,
parser: Polymer.CssParse,
ruleTypes: Polymer.CssParse.types
ruleTypes: Polymer.CssParse.types,

};

Expand Down
2 changes: 1 addition & 1 deletion src/lib/template/dom-bind.html
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
this._prepBindings();
this._prepPropertyInfo();
Polymer.Base._initFeatures.call(this);
this._children = Array.prototype.slice.call(this.root.childNodes);
this._children = Polymer.DomApi.arrayCopyChildNodes(this.root);
}
this._insertChildren();
this.fire('dom-change');
Expand Down
10 changes: 5 additions & 5 deletions src/lib/template/dom-if.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@

_teardownInstance: function() {
if (this._instance) {
var c = this._instance._children;
if (c) {
var c$ = this._instance._children;
if (c$) {
// use first child parent, for case when dom-if may have been detached
var parent = Polymer.dom(Polymer.dom(c[0]).parentNode);
c.forEach(function(n) {
var parent = Polymer.dom(Polymer.dom(c$[0]).parentNode);
for (var i=0, n; (i<c$.length) && (n=c$[i]); i++) {
parent.removeChild(n);
});
}
}
this._instance = null;
}
Expand Down
36 changes: 19 additions & 17 deletions src/lib/template/dom-repeat.html
Original file line number Diff line number Diff line change
Expand Up @@ -420,16 +420,16 @@
var sortFn = this._sortFn ||
function(a, b) { return self._keySort(a, b); };
// Dedupe added and removed keys to a final added/removed map
splices.forEach(function(s) {
for (var i=0; i<s.removed.length; i++) {
var key = s.removed[i];
for (var i=0, s; (i<splices.length) && (s=splices[i]); i++) {
for (var j=0; j<s.removed.length; j++) {
var key = s.removed[j];
keyMap[key] = keyMap[key] ? null : -1;
}
for (var i=0; i<s.added.length; i++) {
var key = s.added[i];
for (var j=0; j<s.added.length; j++) {
var key = s.added[j];
keyMap[key] = keyMap[key] ? null : 1;
}
}, this);
}
// Convert added/removed key map to added/removed arrays
var removedIdxs = [];
var addedKeys = [];
Expand Down Expand Up @@ -514,24 +514,24 @@
_applySplicesArrayOrder: function(splices) {
var pool = [];
var c = this.collection;
splices.forEach(function(s) {
for (var i=0, s; (i<splices.length) && (s=splices[i]); i++) {
// Detach & pool removed instances
for (var i=0; i<s.removed.length; i++) {
var inst = this._detachRow(s.index + i);
for (var j=0; j<s.removed.length; j++) {
var inst = this._detachRow(s.index + j);
if (!inst.isPlaceholder) {
pool.push(inst);
}
}
this._instances.splice(s.index, s.removed.length);
// Insert placeholders for new rows
for (var i=0; i<s.addedKeys.length; i++) {
for (var j=0; j<s.addedKeys.length; j++) {
var inst = {
isPlaceholder: true,
key: s.addedKeys[i]
key: s.addedKeys[j]
};
this._instances.splice(s.index + i, 0, inst);
this._instances.splice(s.index + j, 0, inst);
}
}, this);
}
// Replace placeholders with actual instances (from pool or newly created)
// Iterate backwards to ensure insertBefore refrence is never a placeholder
for (var i=this._instances.length-1; i>=0; i--) {
Expand Down Expand Up @@ -619,18 +619,20 @@
// Called as side-effect of a host property change, responsible for
// notifying parent path change on each inst
_forwardParentProp: function(prop, value) {
this._instances.forEach(function(inst) {
var i$ = this._instances;
for (var i=0, inst; (i<i$.length) && (inst=i$[i]); i++) {
inst.__setProperty(prop, value, true);
}, this);
}
},

// Implements extension point from Templatizer
// Called as side-effect of a host path change, responsible for
// notifying parent path change on each inst
_forwardParentPath: function(path, value) {
this._instances.forEach(function(inst) {
var i$ = this._instances;
for (var i=0, inst; (i<i$.length) && (inst=i$[i]); i++) {
inst._notifyPath(path, value, true);
}, this);
}
},

// Called as a side effect of a host items.<key>.<path> path change,
Expand Down
12 changes: 6 additions & 6 deletions src/standard/annotations.html
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@
// templates.
_discoverTemplateParentProps: function(notes) {
var pp = {};
notes.forEach(function(n) {
for (var i=0, n; (i<notes.length) && (n=notes[i]); i++) {
// Find all bindings to parent.* and spread them into _parentPropChain
n.bindings.forEach(function(b) {
b.parts.forEach(function(p) {
for (var j=0, b$=n.bindings, b; (j<b$.length) && (b=b$[j]); j++) {
for (var k=0, p$=b.parts, p; (k<p$.length) && (p=p$[k]); k++) {
if (p.signature) {
var args = p.signature.args;
for (var k=0; k<args.length; k++) {
Expand All @@ -193,14 +193,14 @@
} else {
pp[p.model] = true;
}
});
});
}
}
// Merge child _parentProps into this _parentProps
if (n.templateContent) {
var tpp = n.templateContent._parentProps;
Polymer.Base.mixin(pp, tpp);
}
});
}
return pp;
},

Expand Down
9 changes: 5 additions & 4 deletions src/standard/notify-path.html
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,15 @@
// get root property
var model = this._modelForPath(path);
// search property effects of the root property for 'annotation' effects
var fx$ = this._propertyEffects[model];
var fx$ = this._propertyEffects && this._propertyEffects[model];
if (fx$) {
fx$.forEach(function(fx) {
var fxFn = this['_' + fx.kind + 'PathEffect'];
for (var i=0, fx; (i<fx$.length) && (fx=fx$[i]); i++) {
// use memoized path functions
var fxFn = fx.pathFn;
if (fxFn) {
fxFn.call(this, path, value, fx.effect);
}
}, this);
}
}
// notify runtime-bound paths
if (this._boundPaths) {
Expand Down
8 changes: 4 additions & 4 deletions src/standard/styling.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,21 @@
if (node.nodeType === Node.ELEMENT_NODE) {
node.className = self._scopeElementClass(node, node.className);
var n$ = node.querySelectorAll('*');
Array.prototype.forEach.call(n$, function(n) {
for (var i=0, n; (i<n$.length) && (n=n$[i]); i++) {
n.className = self._scopeElementClass(n, n.className);
});
}
}
};
scopify(container);
if (shouldObserve) {
var mo = new MutationObserver(function(mxns) {
mxns.forEach(function(m) {
for (var i=0, m; (i<mxns.length) && (m=mxns[i]); i++) {
if (m.addedNodes) {
for (var i=0; i < m.addedNodes.length; i++) {
scopify(m.addedNodes[i]);
}
}
});
}
});
mo.observe(container, {childList: true, subtree: true});
return mo;
Expand Down

0 comments on commit d2c02a9

Please sign in to comment.