From ca67684beafe8de632053a2916a6c97c89a70281 Mon Sep 17 00:00:00 2001 From: Samy Pesse Date: Wed, 2 Nov 2016 23:28:03 +0100 Subject: [PATCH] Improve performances of "getPreviousSibling" and "getNextSibling" with memoization --- src/models/node.js | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/models/node.js b/src/models/node.js index 1c9904396..b2871e82e 100644 --- a/src/models/node.js +++ b/src/models/node.js @@ -670,12 +670,16 @@ const Node = { */ getNextSibling(key) { - const node = this.assertDescendant(key) - return this - .getParent(node) - .nodes - .skipUntil(child => child == node) - .get(1) + key = Normalize.key(key) + + const parent = this.getParent(key) + const after = parent.nodes + .skipUntil(child => child.key == key) + + if (after.size == 0) { + throw new Error(`Could not find a child node with key "${key}".`) + } + return after.get(1) }, /** @@ -822,12 +826,16 @@ const Node = { */ getPreviousSibling(key) { - const node = this.assertDescendant(key) - return this - .getParent(node) - .nodes - .takeUntil(child => child == node) - .last() + key = Normalize.key(key) + const parent = this.getParent(key) + const before = parent.nodes + .takeUntil(child => child.key == key) + + if (before.size == parent.nodes.size) { + throw new Error(`Could not find a child node with key "${key}".`) + } + + return before.last() }, /**