perf: improve memory usage for static vnodes

Use the already mounted nodes as cache instead of separate caching via
template. This reduces memory usage by 30%+ in VitePress.
This commit is contained in:
Evan You
2022-01-16 20:39:55 +08:00
parent f4f0966b33
commit ed9eb62e59
3 changed files with 42 additions and 11 deletions

View File

@@ -82,5 +82,28 @@ describe('runtime-dom: node-ops', () => {
expect((first as Element).namespaceURI).toMatch('svg')
expect((last as Element).namespaceURI).toMatch('svg')
})
test('cached insertion', () => {
const content = `<div>one</div><div>two</div>three`
const existing = `<div>existing</div>`
const parent = document.createElement('div')
parent.innerHTML = existing
const anchor = parent.firstChild
const cached = document.createElement('div')
cached.innerHTML = content
const nodes = nodeOps.insertStaticContent!(
content,
parent,
anchor,
false,
cached.firstChild,
cached.lastChild
)
expect(parent.innerHTML).toBe(content + existing)
expect(nodes[0]).toBe(parent.firstChild)
expect(nodes[1]).toBe(parent.childNodes[parent.childNodes.length - 2])
})
})
})