1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-09 00:36:41 +02:00

change normalize logic to use mutable objects for performance

This commit is contained in:
Ian Storm Taylor
2017-04-02 15:45:48 -07:00
parent 1fb04f663d
commit f81e52a51e

View File

@@ -118,22 +118,28 @@ function normalizeNodeAndChildren(transform, node, schema) {
// We can't just loop the children and normalize them, because in the process // We can't just loop the children and normalize them, because in the process
// of normalizing one child, we might end up creating another. Instead, we // of normalizing one child, we might end up creating another. Instead, we
// have to normalize one at a time, and check for new children along the way. // have to normalize one at a time, and check for new children along the way.
let stack = node.nodes.map(n => n.key).toStack() // PERF: use a mutable array here instead of an immutable stack.
let set = new Set() const keys = node.nodes.toArray().map(n => n.key)
// While there is still a child key that hasn't been normalized yet... // While there is still a child key that hasn't been normalized yet...
while (stack.size) { while (keys.length) {
const ops = transform.operations.length const ops = transform.operations.length
let key let key
// PERF: use a mutable set here since we'll be add to it a lot.
let set = new Set().asMutable()
// Unwind the stack, normalizing every child and adding it to the set. // Unwind the stack, normalizing every child and adding it to the set.
while (key = stack.peek()) { while (key = keys[0]) {
const child = node.getChild(key) const child = node.getChild(key)
normalizeNodeAndChildren(transform, child, schema) normalizeNodeAndChildren(transform, child, schema)
set = set.add(key) set.add(key)
stack = stack.pop() keys.shift()
} }
// Turn the set immutable to be able to compare against it.
set = set.asImmutable()
// PERF: Only re-find the node and re-normalize any new children if // PERF: Only re-find the node and re-normalize any new children if
// operations occured that might have changed it. // operations occured that might have changed it.
if (transform.operations.length != ops) { if (transform.operations.length != ops) {
@@ -142,7 +148,7 @@ function normalizeNodeAndChildren(transform, node, schema) {
// Add any new children back onto the stack. // Add any new children back onto the stack.
node.nodes.forEach((n) => { node.nodes.forEach((n) => {
if (set.has(n.key)) return if (set.has(n.key)) return
stack = stack.push(n.key) keys.unshift(n.key)
}) })
} }
} }