Skip to content

Commit

Permalink
fix $.isEmpty performance (#1096)
Browse files Browse the repository at this point in the history
* fix $.isEmpty performance

* add changelog

* upd bundle

Co-authored-by: Peter Savchenko <specc.dev@gmail.com>
  • Loading branch information
tasuku-s and neSpecc committed Apr 25, 2020
1 parent 7eb642d commit 72213f2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 33 deletions.
2 changes: 1 addition & 1 deletion dist/editor.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- `Fix` - Editor's styles won't be appended to the `<head>` when another instance have already do that [#1079](https://github.com/codex-team/editor.js/issues/1079)
- `New` *I18n API* — Ability to provide internalization for Editor.js core and tools. [#751](https://github.com/codex-team/editor.js/issues/751)
- `Fix` - Fixed wrong toolbar icon centering in Firefox [#1120](https://github.com/codex-team/editor.js/pull/1120)
- `Improvements` - Improve performance of DOM traversing at the `isEmpty()` method [#1095](https://github.com/codex-team/editor.js/issues/1095)
- `Fix` - Toolbox: Tool's order in Toolbox now saved in accordance with `tools` object keys order [#1073](https://github.com/codex-team/editor.js/issues/1073)

### 2.17
Expand Down
38 changes: 6 additions & 32 deletions src/components/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,23 +384,12 @@ export default class Dom {
* @returns {boolean}
*/
public static isEmpty(node: Node): boolean {
const treeWalker = [],
leafs = [];

if (!node) {
return true;
}

if (!node.childNodes.length) {
return this.isNodeEmpty(node);
}

/**
* Normalize node to merge several text nodes to one to reduce tree walker iterations
*/
node.normalize();

treeWalker.push(node.firstChild);
const treeWalker = [ node ];

while (treeWalker.length > 0) {
node = treeWalker.shift();
Expand All @@ -409,31 +398,16 @@ export default class Dom {
continue;
}

if (this.isLeaf(node)) {
leafs.push(node);
} else {
treeWalker.push(node.firstChild);
}

while (node && node.nextSibling) {
node = node.nextSibling;

if (!node) {
continue;
}

treeWalker.push(node);
if (this.isLeaf(node) && !this.isNodeEmpty(node)) {
return false;
}

/**
* If one of child is not empty, checked Node is not empty too
*/
if (node && !this.isNodeEmpty(node)) {
return false;
if (node.childNodes) {
treeWalker.push(...Array.from(node.childNodes));
}
}

return leafs.every((leaf) => this.isNodeEmpty(leaf));
return true;
}

/**
Expand Down

0 comments on commit 72213f2

Please sign in to comment.