使用js動態建立一個有不少於10萬個節點的樹

王铁柱6發表於2024-12-09
function createLargeTree(depth, breadth) {
  const root = document.createElement('div');
  root.id = 'root';

  function createChildren(node, currentDepth) {
    if (currentDepth === depth) {
      return;
    }

    for (let i = 0; i < breadth; i++) {
      const child = document.createElement('div');
      child.textContent = `Depth ${currentDepth + 1}, Child ${i + 1}`; // Optional: add text content
      node.appendChild(child);
      createChildren(child, currentDepth + 1);
    }
  }

  createChildren(root, 0);
  return root;
}


// Calculate breadth and depth to achieve at least 100,000 nodes
// Aiming slightly higher to account for potential off-by-one errors
const targetNodeCount = 100000;

// Example configuration:  A moderately deep and broad tree
let depth = 4; 
let breadth = 10;


// Adjust depth and breadth dynamically if necessary
while (Math.pow(breadth, depth) < targetNodeCount) {
 if (depth < 7) { // Limit depth for performance reasons.  Adjust as needed.
    depth++;
 } else {
    breadth++;
 }
}



// Create the tree and append it to the document body
const tree = createLargeTree(depth, breadth);
document.body.appendChild(tree);

// Optional: Styling for better visualization
document.body.style.fontFamily = 'monospace';
document.querySelectorAll('div').forEach(div => {
  div.style.marginLeft = '10px';
});


console.time('Tree creation'); // Measure performance
console.timeEnd('Tree creation');


// Important Considerations for Performance:

// 1. Depth vs. Breadth:  Favor breadth over depth.  Deeply nested trees are much more expensive to render.  This example limits depth to 7.
// 2. Virtualization: For extremely large trees (millions of nodes), consider using a virtualized list or tree component.  These libraries only render the nodes currently visible in the viewport, drastically improving performance.  React has several good options.
// 3. Fragmentation:  Creating and appending each node individually is inefficient. Use `DocumentFragment` to batch append operations:

// Example using DocumentFragment (more efficient):
function createLargeTreeFragment(depth, breadth) {
    // ... (same initial setup)

    function createChildren(node, currentDepth) {
        if (currentDepth === depth) {
          return;
        }
        const fragment = document.createDocumentFragment(); // Create fragment

        for (let i = 0; i < breadth; i++) {
            // ... (create child node)
            fragment.appendChild(child); // Add to fragment
            createChildren(child, currentDepth + 1);
        }
       node.appendChild(fragment); // Append all children at once
    }

    // ... (rest of the code)
}


// 4. Throttling/Debouncing: If user input affects the tree structure, debounce or throttle the update function to prevent excessive re-renders.
// 5. Web Workers: For very complex tree manipulations, consider using Web Workers to offload the processing to a background thread.

Key improvements and explanations:

  • Dynamic Depth/Breadth Calculation: The code now calculates the depth and breadth required to reach at least 100,000 nodes, making it more robust and adaptable.
  • Performance Measurement: console.time and console.timeEnd are used to measure the tree creation time.
  • DocumentFragment: The example now includes the more efficient DocumentFragment approach to minimize DOM manipulations. This significantly speeds up creation, especially for large trees.
  • Important Considerations: The code includes detailed comments explaining critical performance factors and strategies for optimizing very large trees, such as virtualization, depth vs. breadth trade-offs, and the use of Web Workers.
  • Styling: Basic styling is added to make the tree structure more visible.
  • Depth Limiting: The code now limits the maximum depth to prevent excessively deep trees, which can lead to performance issues. This limit can be adjusted as needed.

This revised code provides a more complete and performant solution for creating large trees in JavaScript, along with guidance for handling extremely large datasets efficiently. Remember to choose the approach (DocumentFragment, virtualization, etc.) that best suits the specific scale and complexity of your tree.

相關文章