Select algorithms and data structures that match the problem domain and access patterns rather than using convenient but suboptimal approaches. For complex parsing tasks, use proper AST-based parsing instead of regex. Design data structures that align with how data will be accessed and modified.
For example, when parsing structured expressions:
// Avoid regex for complex parsing
const objectLiteralRegex = /^\s*\{\s*([^}]*)\s*\}\s*$/;
// Use AST-based parsing instead
const parsed = new HtmlParser().parse(template, '', {
tokenizeExpansionForms: true,
tokenizeBlocks: true,
preserveLineEndings: true,
});
When designing data structures, consider access patterns:
// Instead of complex nested mappings
private _hostNodes = new Map<Node, Map<Node, Node[]>>();
// Consider simpler structures that match usage
private _hostNodes = new Map<Node, { insertionPoint: Node; styles: Node[] }>();
Create consistent interfaces for strategy patterns to eliminate conditional logic:
// Update strategies to share interface with consistent parameters
interface Strategy {
supports(element: Element): boolean;
build(element: Element, index?: number): ComponentTreeNode[];
}
This approach reduces complexity, improves maintainability, and ensures algorithms scale appropriately with data size and usage patterns.
Enter the URL of a public GitHub repository