mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
refactor(compiler): replace flatten and map with flatMap. (#48378)
Replace custom `flatten` and `map` with native `flatMap` usage. Benchmark: | Test case name | Result | |---------------- |-------------------------------------------------------- | | flatten & map | flatten & map x 1,182 ops/sec ±2.18% (63 runs sampled) | | flatMap | flatMap x 6,011 ops/sec ±0.91% (35 runs sampled) | The fact that `flatMap` is faster is also highlighted in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap PR Close #48378
This commit is contained in:
parent
11a24c3740
commit
146d2ee246
1 changed files with 3 additions and 9 deletions
|
|
@ -1393,7 +1393,7 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
|
|||
return o.TYPED_NULL_EXPR;
|
||||
}
|
||||
|
||||
const refsParam = flatten(references.map(reference => {
|
||||
const refsParam = references.flatMap(reference => {
|
||||
const slot = this.allocateDataSlot();
|
||||
// Generate the update temporary.
|
||||
const variableName = this._bindingScope.freshReferenceName();
|
||||
|
|
@ -1410,8 +1410,9 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
|
|||
const refExpr = lhs.set(o.importExpr(R3.reference).callFn([o.literal(slot)]));
|
||||
return nextContextStmt.concat(refExpr.toConstDecl());
|
||||
}, true);
|
||||
|
||||
return [reference.name, reference.value];
|
||||
}));
|
||||
});
|
||||
|
||||
return asLiteral(refsParam);
|
||||
}
|
||||
|
|
@ -2360,10 +2361,3 @@ export interface ParsedTemplate {
|
|||
*/
|
||||
commentNodes?: t.Comment[];
|
||||
}
|
||||
|
||||
function flatten<T>(list: Array<T|T[]>): T[] {
|
||||
return list.reduce((flat: any[], item: T|T[]): T[] => {
|
||||
const flatItem = Array.isArray(item) ? flatten(item) : item;
|
||||
return (<T[]>flat).concat(flatItem);
|
||||
}, []);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue