Skip to content

Commit

Permalink
Fix an accidentally quadratic reduce(concat) in FlattenedNodesObserver (
Browse files Browse the repository at this point in the history
#5739)

Found by @mvanbem-goog debugging a perf issue in Gerrit
  • Loading branch information
rictic authored Sep 16, 2024
1 parent 1e8b246 commit 3c2f759
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## Unreleased
- FlattenedNodesObserver.getFlattenedNodes was accidentally quadratic if given a non-slot node. Found by @mvanbem-goog.

## [v3.5.1](https://github.com/Polymer/polymer/tree/v3.5.1) (2022-06-03)
- [ci skip] bump to 3.5.1 ([commit](https://github.com/Polymer/polymer/commit/2cbb3d2b))

Expand Down
15 changes: 9 additions & 6 deletions lib/utils/flattened-nodes-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,17 @@ export let FlattenedNodesObserver = class {
node = /** @type {!HTMLSlotElement} */(node); // eslint-disable-line no-self-assign
return wrapped.assignedNodes({flatten: true});
} else {
return Array.from(wrapped.childNodes).map((node) => {
const results = [];
for (let i = 0; i < wrapped.childNodes.length; i++) {
const node = wrapped.childNodes[i];
if (isSlot(node)) {
node = /** @type {!HTMLSlotElement} */(node); // eslint-disable-line no-self-assign
return wrap(node).assignedNodes({flatten: true});
const slotNode = /** @type {!HTMLSlotElement} */ (node);
results.push(...wrap(slotNode).assignedNodes({ flatten: true }));
} else {
return [node];
results.push(node);
}
}).reduce((a, b) => a.concat(b), []);
}
return results;
}
}

Expand Down Expand Up @@ -311,4 +314,4 @@ export let FlattenedNodesObserver = class {
}
}

};
};

0 comments on commit 3c2f759

Please sign in to comment.