Skip to content

Commit d2c02a9

Browse files
author
Steven Orvell
committed
Avoid using .slice and .forEach
1 parent 53f3a7d commit d2c02a9

11 files changed

Lines changed: 58 additions & 59 deletions

File tree

‎src/lib/base.html‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@
7878
*/
7979
extend: function(prototype, api) {
8080
if (prototype && api) {
81-
Object.getOwnPropertyNames(api).forEach(function(n) {
81+
var n$ = Object.getOwnPropertyNames(api);
82+
for (var i=0, n; (i<n$.length) && (n=n$[i]); i++) {
8283
this.copyOwnProperty(n, api, prototype);
83-
}, this);
84+
}
8485
}
8586
return prototype || api;
8687
},

‎src/lib/collection.html‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@
132132
_applySplices: function(splices) {
133133
// Dedupe added and removed keys to a final added/removed map
134134
var keyMap = {}, key, i;
135-
splices.forEach(function(s) {
135+
for (var i=0, s; (i<splices.length) && (s=splices[i]); i++) {
136136
s.addedKeys = [];
137-
for (i=0; i<s.removed.length; i++) {
138-
key = this.getKey(s.removed[i]);
137+
for (j=0; j<s.removed.length; j++) {
138+
key = this.getKey(s.removed[j]);
139139
keyMap[key] = keyMap[key] ? null : -1;
140140
}
141-
for (i=0; i<s.addedCount; i++) {
142-
var item = this.userArray[s.index + i];
141+
for (j=0; j<s.addedCount; j++) {
142+
var item = this.userArray[s.index + j];
143143
key = this.getKey(item);
144144
key = (key === undefined) ? this.add(item) : key;
145145
keyMap[key] = keyMap[key] ? null : 1;
@@ -148,7 +148,7 @@
148148
// further changes to the array by the time the splice is consumed
149149
s.addedKeys.push(key);
150150
}
151-
}, this);
151+
}
152152
// Convert added/removed key map to added/removed arrays
153153
var removed = [];
154154
var added = [];

‎src/lib/dom-api.html‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -791,15 +791,6 @@
791791

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

794-
var nativeForwards = ['appendChild', 'insertBefore',
795-
'removeChild', 'replaceChild'];
796-
797-
nativeForwards.forEach(function(forward) {
798-
DomApi.prototype[forward] = function() {
799-
return this.node[forward].apply(this.node, arguments);
800-
};
801-
});
802-
803794
Object.defineProperties(DomApi.prototype, {
804795

805796
childNodes: {
@@ -998,7 +989,10 @@
998989
hasInsertionPoint: hasInsertionPoint,
999990
ctor: DomApi,
1000991
factory: factory,
1001-
hasDomApi: hasDomApi
992+
hasDomApi: hasDomApi,
993+
arrayCopy: arrayCopy,
994+
arrayCopyChildNodes: arrayCopyChildNodes,
995+
arrayCopyChildren: arrayCopyChildren
1002996
};
1003997

1004998
})();

‎src/lib/settings.html‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
var user = window.Polymer || {};
2222

2323
// via url
24-
location.search.slice(1).split('&').forEach(function(o) {
24+
var parts = location.search.slice(1).split('&');
25+
for (var i=0, o; (i < parts.length) && (o=parts[i]); i++) {
2526
o = o.split('=');
2627
o[0] && (user[o[0]] = o[1] || true);
27-
});
28+
}
2829

2930
var wantShadow = (user.dom === 'shadow');
3031
var hasShadow = Boolean(Element.prototype.createShadowRoot);

‎src/lib/style-util.html‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
var cssText = '';
113113
// if element is a template, get content from its .content
114114
var content = element.content || element;
115-
var e$ = Array.prototype.slice.call(
115+
var e$ = Polymer.DomApi.arrayCopy(
116116
content.querySelectorAll(this.MODULE_STYLES_SELECTOR));
117117
for (var i=0, e; i < e$.length; i++) {
118118
e = e$[i];
@@ -145,7 +145,7 @@
145145

146146
resolveCss: Polymer.ResolveUrl.resolveCss,
147147
parser: Polymer.CssParse,
148-
ruleTypes: Polymer.CssParse.types
148+
ruleTypes: Polymer.CssParse.types,
149149

150150
};
151151

‎src/lib/template/dom-bind.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
this._prepBindings();
154154
this._prepPropertyInfo();
155155
Polymer.Base._initFeatures.call(this);
156-
this._children = Array.prototype.slice.call(this.root.childNodes);
156+
this._children = Polymer.DomApi.arrayCopyChildNodes(this.root);
157157
}
158158
this._insertChildren();
159159
this.fire('dom-change');

‎src/lib/template/dom-if.html‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@
127127

128128
_teardownInstance: function() {
129129
if (this._instance) {
130-
var c = this._instance._children;
131-
if (c) {
130+
var c$ = this._instance._children;
131+
if (c$) {
132132
// use first child parent, for case when dom-if may have been detached
133-
var parent = Polymer.dom(Polymer.dom(c[0]).parentNode);
134-
c.forEach(function(n) {
133+
var parent = Polymer.dom(Polymer.dom(c$[0]).parentNode);
134+
for (var i=0, n; (i<c$.length) && (n=c$[i]); i++) {
135135
parent.removeChild(n);
136-
});
136+
}
137137
}
138138
this._instance = null;
139139
}

‎src/lib/template/dom-repeat.html‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -420,16 +420,16 @@
420420
var sortFn = this._sortFn ||
421421
function(a, b) { return self._keySort(a, b); };
422422
// Dedupe added and removed keys to a final added/removed map
423-
splices.forEach(function(s) {
424-
for (var i=0; i<s.removed.length; i++) {
425-
var key = s.removed[i];
423+
for (var i=0, s; (i<splices.length) && (s=splices[i]); i++) {
424+
for (var j=0; j<s.removed.length; j++) {
425+
var key = s.removed[j];
426426
keyMap[key] = keyMap[key] ? null : -1;
427427
}
428-
for (var i=0; i<s.added.length; i++) {
429-
var key = s.added[i];
428+
for (var j=0; j<s.added.length; j++) {
429+
var key = s.added[j];
430430
keyMap[key] = keyMap[key] ? null : 1;
431431
}
432-
}, this);
432+
}
433433
// Convert added/removed key map to added/removed arrays
434434
var removedIdxs = [];
435435
var addedKeys = [];
@@ -514,24 +514,24 @@
514514
_applySplicesArrayOrder: function(splices) {
515515
var pool = [];
516516
var c = this.collection;
517-
splices.forEach(function(s) {
517+
for (var i=0, s; (i<splices.length) && (s=splices[i]); i++) {
518518
// Detach & pool removed instances
519-
for (var i=0; i<s.removed.length; i++) {
520-
var inst = this._detachRow(s.index + i);
519+
for (var j=0; j<s.removed.length; j++) {
520+
var inst = this._detachRow(s.index + j);
521521
if (!inst.isPlaceholder) {
522522
pool.push(inst);
523523
}
524524
}
525525
this._instances.splice(s.index, s.removed.length);
526526
// Insert placeholders for new rows
527-
for (var i=0; i<s.addedKeys.length; i++) {
527+
for (var j=0; j<s.addedKeys.length; j++) {
528528
var inst = {
529529
isPlaceholder: true,
530-
key: s.addedKeys[i]
530+
key: s.addedKeys[j]
531531
};
532-
this._instances.splice(s.index + i, 0, inst);
532+
this._instances.splice(s.index + j, 0, inst);
533533
}
534-
}, this);
534+
}
535535
// Replace placeholders with actual instances (from pool or newly created)
536536
// Iterate backwards to ensure insertBefore refrence is never a placeholder
537537
for (var i=this._instances.length-1; i>=0; i--) {
@@ -619,18 +619,20 @@
619619
// Called as side-effect of a host property change, responsible for
620620
// notifying parent path change on each inst
621621
_forwardParentProp: function(prop, value) {
622-
this._instances.forEach(function(inst) {
622+
var i$ = this._instances;
623+
for (var i=0, inst; (i<i$.length) && (inst=i$[i]); i++) {
623624
inst.__setProperty(prop, value, true);
624-
}, this);
625+
}
625626
},
626627

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

636638
// Called as a side effect of a host items.<key>.<path> path change,

‎src/standard/annotations.html‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@
181181
// templates.
182182
_discoverTemplateParentProps: function(notes) {
183183
var pp = {};
184-
notes.forEach(function(n) {
184+
for (var i=0, n; (i<notes.length) && (n=notes[i]); i++) {
185185
// Find all bindings to parent.* and spread them into _parentPropChain
186-
n.bindings.forEach(function(b) {
187-
b.parts.forEach(function(p) {
186+
for (var j=0, b$=n.bindings, b; (j<b$.length) && (b=b$[j]); j++) {
187+
for (var k=0, p$=b.parts, p; (k<p$.length) && (p=p$[k]); k++) {
188188
if (p.signature) {
189189
var args = p.signature.args;
190190
for (var k=0; k<args.length; k++) {
@@ -193,14 +193,14 @@
193193
} else {
194194
pp[p.model] = true;
195195
}
196-
});
197-
});
196+
}
197+
}
198198
// Merge child _parentProps into this _parentProps
199199
if (n.templateContent) {
200200
var tpp = n.templateContent._parentProps;
201201
Polymer.Base.mixin(pp, tpp);
202202
}
203-
});
203+
}
204204
return pp;
205205
},
206206

‎src/standard/notify-path.html‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,15 @@
271271
// get root property
272272
var model = this._modelForPath(path);
273273
// search property effects of the root property for 'annotation' effects
274-
var fx$ = this._propertyEffects[model];
274+
var fx$ = this._propertyEffects && this._propertyEffects[model];
275275
if (fx$) {
276-
fx$.forEach(function(fx) {
277-
var fxFn = this['_' + fx.kind + 'PathEffect'];
276+
for (var i=0, fx; (i<fx$.length) && (fx=fx$[i]); i++) {
277+
// use memoized path functions
278+
var fxFn = fx.pathFn;
278279
if (fxFn) {
279280
fxFn.call(this, path, value, fx.effect);
280281
}
281-
}, this);
282+
}
282283
}
283284
// notify runtime-bound paths
284285
if (this._boundPaths) {

0 commit comments

Comments
 (0)