1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-04-12 01:11:53 +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
// 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.
let stack = node.nodes.map(n => n.key).toStack()
let set = new Set()
// PERF: use a mutable array here instead of an immutable stack.
const keys = node.nodes.toArray().map(n => n.key)
// While there is still a child key that hasn't been normalized yet...
while (stack.size) {
while (keys.length) {
const ops = transform.operations.length
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.
while (key = stack.peek()) {
while (key = keys[0]) {
const child = node.getChild(key)
normalizeNodeAndChildren(transform, child, schema)
set = set.add(key)
stack = stack.pop()
set.add(key)
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
// operations occured that might have changed it.
if (transform.operations.length != ops) {
@ -142,7 +148,7 @@ function normalizeNodeAndChildren(transform, node, schema) {
// Add any new children back onto the stack.
node.nodes.forEach((n) => {
if (set.has(n.key)) return
stack = stack.push(n.key)
keys.unshift(n.key)
})
}
}