From 3c2f7591c5235f538f915eb61e90b1d8011c234a Mon Sep 17 00:00:00 2001 From: Peter Burns Date: Mon, 16 Sep 2024 15:13:03 -0700 Subject: [PATCH] Fix an accidentally quadratic reduce(concat) in FlattenedNodesObserver (#5739) Found by @mvanbem-goog debugging a perf issue in Gerrit --- CHANGELOG.md | 3 +++ lib/utils/flattened-nodes-observer.js | 15 +++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79f30f27f0..f7270b3324 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/lib/utils/flattened-nodes-observer.js b/lib/utils/flattened-nodes-observer.js index 920ef9a6f7..24a5156bac 100644 --- a/lib/utils/flattened-nodes-observer.js +++ b/lib/utils/flattened-nodes-observer.js @@ -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; } } @@ -311,4 +314,4 @@ export let FlattenedNodesObserver = class { } } -}; \ No newline at end of file +};