This JavaScript utility extracts all unique DOM element types that appear within the <body> element of a webpage. The code consists of two main parts:
- Polyfill for Array.prototype.findIndex() - Provides backward compatibility for older browsers
- getUniqueDomElements function - Main functionality that returns an array of unique DOM element tag names found within the body
- Get All Elements: Uses
document.getElementsByTagName("*")to retrieve all DOM elements on the page - Convert to Array: Converts the HTMLCollection to a proper array using
Array.prototype.slice.call() - Extract Tag Names: Maps each element to its
nodeNameproperty (e.g., "DIV", "P", "SPAN") - Filter for Uniqueness: Uses
Array.filter()withindexOf()to remove duplicates - Find Body Index: Locates the position of the "BODY" element in the unique elements array
- Extract Body Children: Returns only elements that appear after the BODY element in the array
// For a page with <div>, <p>, <span>, <img> elements within body
getUniqueDomElements(); // Returns: ["DIV", "P", "SPAN", "IMG"]-
Flawed Core Logic: The assumption that elements appearing after "BODY" in the unique array are necessarily children of BODY is incorrect. The function doesn't actually check DOM hierarchy.
-
Misleading Results: May include elements outside of
<body>(like<head>elements) if they happen to appear later in the alphabetical/document order. -
No Error Handling: No validation for edge cases or error scenarios.
- Unnecessary Polyfill:
findIndex()has been widely supported since ES2015 (2015) - over 8 years ago - Verbose Implementation: Multiple intermediate variables and loops where modern methods would be cleaner
- Poor Variable Naming: Names like
domArrayanddomElementNamesare redundant - Inefficient Filtering: Uses
indexOf()for uniqueness, which is O(n²) complexity
Here's a modern, corrected version that actually fulfills the intended purpose:
const getUniqueDomElements = () => {
const bodyElements = document.body?.querySelectorAll('*') || [];
const uniqueTagNames = [...new Set([...bodyElements].map(el => el.tagName))];
return uniqueTagNames.sort();
};const getUniqueDomElements = (options = {}) => {
const {
container = document.body,
includeContainer = false,
toLowerCase = false,
sortResults = true
} = options;
if (!container) {
throw new Error('Container element not found');
}
const elements = container.querySelectorAll('*');
const tagNames = [...elements].map(el =>
toLowerCase ? el.tagName.toLowerCase() : el.tagName
);
if (includeContainer) {
tagNames.push(toLowerCase ? container.tagName.toLowerCase() : container.tagName);
}
const unique = [...new Set(tagNames)];
return sortResults ? unique.sort() : unique;
};const getUniqueDomElements = (includeMetadata = false) => {
const bodyElements = document.body?.querySelectorAll('*') || [];
const elementMap = new Map();
bodyElements.forEach(element => {
const tagName = element.tagName;
if (!elementMap.has(tagName)) {
elementMap.set(tagName, {
tagName,
count: 0,
firstInstance: element
});
}
elementMap.get(tagName).count++;
});
return includeMetadata
? Array.from(elementMap.values()).sort((a, b) => a.tagName.localeCompare(b.tagName))
: Array.from(elementMap.keys()).sort();
};- Fix Core Logic: Use
document.body.querySelectorAll('*')to actually target body descendants - Remove Polyfill: Eliminate the
findIndexpolyfill for modern browsers - Use Modern Syntax: Leverage ES6+ features like Set, spread operator, and arrow functions
- Add Error Handling: Check if
document.bodyexists before proceeding
- Configurable Container: Allow targeting elements other than body
- Case Sensitivity Options: Provide lowercase/uppercase options
- Metadata Support: Include element counts and first instances
- Performance Optimization: Use Set for O(1) uniqueness checking
- ✅ Single responsibility principle
- ✅ Proper error handling
- ✅ Modern JavaScript features
- ✅ Clear, descriptive naming
- ✅ Configurable behavior
- ✅ Efficient algorithms
While the original code demonstrates basic DOM manipulation concepts, it suffers from fundamental logical flaws and outdated practices. The improved versions provide correct functionality, better performance, and modern JavaScript patterns while maintaining simplicity and extensibility.