Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Early Bailout & Smart Node Processing; Batch DOM Operations: - amazon full 2.2 seconds
Magnus Müller4457ae1b04e1
diff --git a/browser_use/dom/buildDomTree.js b/browser_use/dom/buildDomTree.jsindex 94597ce49..eeb38b6c5 100644--- a/browser_use/dom/buildDomTree.js+++ b/browser_use/dom/buildDomTree.js@@ -634,6 +634,38 @@}, 'scrollOperations');}+ // Add these helper functions at the top level+ function isInteractiveCandidate(element) {+ if (!element || element.nodeType !== Node.ELEMENT_NODE) return false;++ const tagName = element.tagName.toLowerCase();++ // Fast-path for common interactive elements+ const interactiveElements = new Set([+ "a", "button", "input", "select", "textarea", "details", "summary"+ ]);++ if (interactiveElements.has(tagName)) return true;++ // Quick attribute checks without getting full lists+ const hasQuickInteractiveAttr = element.hasAttribute("onclick") ||+ element.hasAttribute("role") ||+ element.hasAttribute("tabindex") ||+ element.hasAttribute("aria-") ||+ element.hasAttribute("data-action");++ return hasQuickInteractiveAttr;+ }++ function quickVisibilityCheck(element) {+ // Fast initial check before expensive getComputedStyle+ return element.offsetWidth > 0 &&+ element.offsetHeight > 0 &&+ !element.hasAttribute("hidden") &&+ element.style.display !== "none" &&+ element.style.visibility !== "hidden";+ }+/*** Creates a node data object for a given node and its descendants and returns* the identifier of the node in the hash map or null if the node is not accepted.@@ -649,101 +681,75 @@let result;try {- if (!node) {- return null;- }+ if (!node) return null;++ // Skip highlight container+ if (node.id === HIGHLIGHT_CONTAINER_ID) return null;++ // Early bailout for non-element nodes except text+ if (node.nodeType !== Node.ELEMENT_NODE && node.nodeType !== Node.TEXT_NODE) return null;++ // Quick checks for element nodes+ if (node.nodeType === Node.ELEMENT_NODE) {+ // Skip denied elements early+ if (!isElementAccepted(node)) return null;++ // Quick visibility check before expensive operations+ if (!quickVisibilityCheck(node)) return null;- // NOTE: We skip highlight container nodes from the DOM tree- if (node.id === HIGHLIGHT_CONTAINER_ID) {- return null;+ // If not in viewport and not processing all elements, skip+ if (viewportExpansion !== -1) {+ const rect = getCachedBoundingRect(node);+ if (rect.bottom < -viewportExpansion ||+ rect.top > window.innerHeight + viewportExpansion ||+ rect.right < -viewportExpansion ||+ rect.left > window.innerWidth + viewportExpansion) {+ return null;+ }+ }}- // Special case for text nodes+ // Process text nodesif (node.nodeType === Node.TEXT_NODE) {return measureBuildDomTreePart('textNodeProcessing', () => {const textContent = node.textContent.trim();- if (textContent) {- const id = `${ID.current++}`;- DOM_HASH_MAP[id] = {- type: "TEXT_NODE",- text: textContent,- isVisible: isTextNodeVisible(node),- };- return id;- }- return null;- });- }+ if (!textContent) return null;- // Check if element is accepted- if (node.nodeType === Node.ELEMENT_NODE && !isElementAccepted(node)) {- return null;+ const id = `${ID.current++}`;+ DOM_HASH_MAP[id] = {+ type: "TEXT_NODE",+ text: textContent,+ isVisible: isTextNodeVisible(node),+ };+ return id;+ });}+ // At this point we know it's an element node that passed initial checksconst nodeData = measureBuildDomTreePart('elementNodeProcessing', () => ({- tagName: node.tagName ? node.tagName.toLowerCase() : null,+ tagName: node.tagName.toLowerCase(),attributes: {},- xpath: node.nodeType === Node.ELEMENT_NODE ? getXPathTree(node, true) : null,+ xpath: getXPathTree(node, true),children: [],}));- // Add coordinates for element nodes- if (node.nodeType === Node.ELEMENT_NODE) {- measureBuildDomTreePart('attributeProcessing', () => {- const rect = getCachedBoundingRect(node);-- // Only store minimal viewport info- nodeData.viewport = {- width: window.innerWidth,- height: window.innerHeight- };- });- }-- // Measure attribute operations- measureBuildDomTreePart('attributeProcessing', () => {- const attributeNames = measureDomOperation(- () => node.getAttributeNames?.() || [],- 'getAttributeNames'- );- for (const name of attributeNames) {- nodeData.attributes[name] = measureDomOperation(- () => node.getAttribute(name),- 'getAttribute'- );- }- });+ // Only do expensive checks if element is a potential interactive candidate+ const isCandidate = isInteractiveCandidate(node);- if (node.nodeType === Node.ELEMENT_NODE) {+ if (isCandidate) {measureBuildDomTreePart('interactivityChecks', () => {- // Initialize all flags to false by default- nodeData.isInteractive = false;- nodeData.isVisible = false;- nodeData.isTopElement = false;- nodeData.isInViewport = false;-- // Check visibility first as it's the fastest check- const isVisible = isElementVisible(node);- if (!isVisible) return;- nodeData.isVisible = true;-- // Check interactivity next as it's the second fastest- const isInteractive = isInteractiveElement(node);- if (!isInteractive) return;- nodeData.isInteractive = true;-- // Check if element is top as third fastest- const isTop = isTopElement(node);- if (!isTop) return;- nodeData.isTopElement = true;-- // Check viewport last as it's the most expensive- const isInViewport = isInExpandedViewport(node, viewportExpansion);- if (!isInViewport) return;- nodeData.isInViewport = true;-- // Only if all checks pass, do we handle highlighting+ nodeData.isVisible = isElementVisible(node);+ if (!nodeData.isVisible) return;++ nodeData.isInteractive = isInteractiveElement(node);+ if (!nodeData.isInteractive) return;++ nodeData.isTopElement = isTopElement(node);+ if (!nodeData.isTopElement) return;++ nodeData.isInViewport = true; // We already checked thisnodeData.highlightIndex = highlightIndex++;+if (doHighlightElements) {if (focusHighlightIndex >= 0) {if (focusHighlightIndex === nodeData.highlightIndex) {@@ -756,18 +762,9 @@});}- // Handle shadow DOM and iframes+ // Process childrenmeasureBuildDomTreePart('childrenProcessing', () => {- if (node.shadowRoot) {- nodeData.shadowRoot = true;- for (const child of node.shadowRoot.childNodes) {- const domElement = buildDomTree(child, parentIframe);- if (domElement) {- nodeData.children.push(domElement);- }- }- }-+ // Only process children if parent might contain interactive elementsif (node.tagName && node.tagName.toLowerCase() === "iframe") {measureBuildDomTreePart('iframeProcessing', () => {try {@@ -775,9 +772,7 @@if (iframeDoc) {for (const child of iframeDoc.childNodes) {const domElement = buildDomTree(child, node);- if (domElement) {- nodeData.children.push(domElement);- }+ if (domElement) nodeData.children.push(domElement);}}} catch (e) {@@ -787,15 +782,9 @@} else {for (const child of node.childNodes) {const domElement = buildDomTree(child, parentIframe);- if (domElement) {- nodeData.children.push(domElement);- }+ if (domElement) nodeData.children.push(domElement);}}-- if (nodeData.tagName === 'a' && nodeData.children.length === 0) {- return null;- }});result = measureBuildDomTreePart('domHashMapOperations', () => {