Performance: Graph nodes and edges
MEDIUM IMPACT
This concept affects how quickly graph data structures load and update in the UI, impacting interaction responsiveness and rendering speed.
const fragment = document.createDocumentFragment(); for (const node of graph.nodes) { const nodeElement = document.createElement('div'); nodeElement.textContent = node.label; fragment.appendChild(nodeElement); } for (const edge of graph.edges) { const edgeElement = document.createElement('div'); edgeElement.textContent = `${edge.source} -> ${edge.target}`; fragment.appendChild(edgeElement); } document.body.appendChild(fragment);
for (const node of graph.nodes) { const nodeElement = document.createElement('div'); nodeElement.textContent = node.label; document.body.appendChild(nodeElement); for (const edge of node.edges) { const edgeElement = document.createElement('div'); edgeElement.textContent = `${node.label} -> ${edge.target}`; document.body.appendChild(edgeElement); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Appending nodes and edges one by one | Many individual insertions | N + M reflows | High paint cost due to repeated repaints | [X] Bad |
| Batch appending nodes and edges with DocumentFragment | Single batch insertion | 1 reflow | Low paint cost with single repaint | [OK] Good |