From cb333a4882652a5a1b6cb33cea27c1dd72bbe955 Mon Sep 17 00:00:00 2001 From: Soreine Date: Wed, 2 Nov 2016 12:27:56 +0100 Subject: [PATCH] Rewrite getDescendant to be recursively memoized --- src/models/node.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/models/node.js b/src/models/node.js index 56af09654..0147d9671 100644 --- a/src/models/node.js +++ b/src/models/node.js @@ -434,8 +434,25 @@ const Node = { getDescendant(key) { key = Normalize.key(key) + return this._getDescendant(key) + }, - return this.findDescendantDeep(node => node.key == key) + // This one is memoized + _getDescendant(key) { + let descendantFound = null + + const found = this.nodes.find(node => { + if (node.key === key) { + return node + } else if (node.kind !== 'text') { + descendantFound = node._getDescendant(key) + return descendantFound + } else { + return false + } + }) + + return descendantFound || found }, /** @@ -1316,7 +1333,7 @@ memoize(Node, [ 'getComponent', 'getDecorators', 'getDepth', - 'getDescendant', + '_getDescendant', 'getDescendantAtPath', 'getDescendantDecorators', 'getFirstText',