8000 Cleanup `iteratorTemplate` and optimize methods that use `mapIterator… · lodash/lodash@f5f2bf7 · GitHub
[go: up one dir, main page]

Skip to content

Commit f5f2bf7

Browse files
committed
Cleanup iteratorTemplate and optimize methods that use mapIteratorOptions.
Former-commit-id: a0f876250a1c7875745e9080bf75546e16fbf6e7
1 parent acb6656 commit f5f2bf7

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

lodash.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -276,40 +276,39 @@
276276
' while (<%= arrayBranch.loopExp %>) {\n' +
277277
' <%= arrayBranch.inLoop %>\n' +
278278
' }' +
279-
' <% if (objectBranch) { %>\n}\n<% }' +
280-
'}' +
279+
' <% if (objectBranch) { %>\n}<% } %>' +
280+
'<% } %>' +
281281

282282
// the following branch is for iterating an object's own/inherited properties
283-
'if (objectBranch) {' +
284-
' if (arrayBranch) { %>else {\n<% }' +
285-
' if (!hasDontEnumBug) { %>' +
283+
'<% if (objectBranch) { %>' +
284+
' <% if (arrayBranch) { %>\nelse {<% } %>' +
285+
' <% if (!hasDontEnumBug) { %>\n' +
286286
' var skipProto = typeof <%= iteratedObject %> == \'function\' && \n' +
287-
' propertyIsEnumerable.call(<%= iteratedObject %>, \'prototype\');\n<%' +
288-
' } %>' +
289-
' <%= objectBranch.beforeLoop %>;\n<%' +
287+
' propertyIsEnumerable.call(<%= iteratedObject %>, \'prototype\');\n' +
288+
' <% } %>' +
290289

291290
// iterate own properties using `Object.keys` if it's fast
292-
' if (isKeysFast && useHas) { %>' +
291+
' <% if (isKeysFast && useHas) { %>\n' +
293292
' var props = nativeKeys(<%= iteratedObject %>),\n' +
294293
' propIndex = -1,\n' +
295-
' length = props.length;\n' +
294+
' length = props.length;\n\n' +
295+
' <%= objectBranch.beforeLoop %>;\n' +
296296
' while (++propIndex < length) {\n' +
297297
' index = props[propIndex];\n' +
298298
' if (!(skipProto && index == \'prototype\')) {\n' +
299299
' <%= objectBranch.inLoop %>\n' +
300300
' }\n' +
301-
' }\n' +
301+
' }' +
302302

303303
// else using a for-in loop
304-
' <% } else { %>' +
304+
' <% } else { %>\n' +
305+
' <%= objectBranch.beforeLoop %>;\n' +
305306
' for (<%= objectBranch.loopExp %>) {' +
306-
' \n<%' +
307-
' if (hasDontEnumBug) {' +
308-
' if (useHas) { %> if (<%= hasExp %>) {\n <% } %>' +
309-
' <%= objectBranch.inLoop %>;<%' +
310-
' if (useHas) { %>\n }<% }' +
311-
' }' +
312-
' else { %>' +
307+
' <% if (hasDontEnumBug) { %>\n' +
308+
' <% if (useHas) { %>if (<%= hasExp %>) {\n <% } %>' +
309+
' <%= objectBranch.inLoop %>;\n' +
310+
' <% if (useHas) { %>}<% } %>' +
311+
' <% } else { %>\n' +
313312

314313
// Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1
315314
// (if the prototype or a property on the prototype has been set)
@@ -328,7 +327,7 @@
328327
// existing property and the `constructor` property of a prototype
329328
// defaults to non-enumerable, Lo-Dash skips the `constructor`
330329
// property when it infers it's iterating over a `prototype` object.
331-
' <% if (hasDontEnumBug) { %>\n' +
330+
' <% if (hasDontEnumBug) { %>\n\n' +
332331
' var ctor = <%= iteratedObject %>.constructor;\n' +
333332
' <% for (var k = 0; k < 7; k++) { %>\n' +
334333
' index = \'<%= shadowed[k] %>\';\n' +
@@ -337,11 +336,11 @@
337336
' %>!(ctor && ctor.prototype === <%= iteratedObject %>) && <%' +
338337
' } %><%= hasExp %>) {\n' +
339338
' <%= objectBranch.inLoop %>\n' +
340-
' }<%' +
341-
' }' +
342-
' }' +
343-
' if (arrayBranch) { %>\n}<% }' +
344-
'} %>\n' +
339+
' }' +
340+
' <% } %>' +
341+
' <% } %>' +
342+
' <% if (arrayBranch) { %>\n}<% } %>' +
343+
'<% } %>\n' +
345344

346345
// add code to the bottom of the iteration function
347346
'<%= bottom %>;\n' +
@@ -411,11 +410,11 @@
411410
'exit': 'if (!collection) return []',
412411
'beforeLoop': {
413412
'array': 'result = Array(length)',
414-
'object': 'result = []'
413+
'object': 'result = ' + (isKeysFast ? 'Array(length)' : '[]')
415414
},
416415
'inLoop': {
417416
'array': 'result[index] = callback(collection[index], index, collection)',
418-
'object': 'result.push(callback(collection[index], index, collection))'
417+
'object': 'result' + (isKeysFast ? '[propIndex] = ' : '.push') + '(callback(collection[index], index, collection))'
419418
}
420419
};
421420

@@ -867,7 +866,7 @@
867866
' isFunc = typeof methodName == \'function\'',
868867
'inLoop': {
869868
'array': 'result[index] = (isFunc ? methodName : collection[index][methodName]).apply(collection[index], args)',
870-
'object': 'result.push((isFunc ? methodName : collection[index][methodName]).apply(collection[index], args))'
869+
'object': 'result' + (isKeysFast ? '[propIndex] = ' : '.push') + '((isFunc ? methodName : collection[index][methodName]).apply(collection[index], args))'
871870
}
872871
});
873872

@@ -920,7 +919,7 @@
920919
'args': 'collection, property',
921920
'inLoop': {
922921
'array': 'result[index] = collection[index][property]',
923-
'object': 'result.push(collection[index][property])'
922+
'object': 'result' + (isKeysFast ? '[propIndex] = ' : '.push') + '(collection[index][property])'
924923
}
925924
});
926925

@@ -1106,7 +1105,7 @@
11061105
' value: collection[index]\n' +
11071106
'}',
11081107
'object':
1109-
'result.push({\n' +
1108+
'result' + (isKeysFast ? '[propIndex] = ' : '.push') + '({\n' +
11101109
' criteria: callback(collection[index], index, collection),\n' +
11111110
' value: collection[index]\n' +
11121111
'})'

0 commit comments

Comments
 (0)
0