{"ScriptPreparationCode":"class FastQueue {\r\n\t\r\n\tconstructor() {\r\n\t\tthis._array = new Array(16);\r\n\t\tthis._length = 0;\r\n\t\tthis._front = 0;\r\n\t}\r\n\t\r\n\tpush(value) {\r\n\t\tconst arr = this._array;\r\n\t\tif (arr.length === this._length) {\r\n\t\t\tarr.length *= 2;\r\n\t\t\tarrayMove(arr, this._length, this._front);\r\n\t\t}\r\n\t\tarr[(this._front \u002B this._length\u002B\u002B) \u0026 (arr.length - 1)] = value;\r\n\t}\r\n\t\r\n\tshift() {\r\n\t\tif (this._length === 0) return;\r\n\t\tconst arr = this._array;\r\n\t\tconst frontIndex = this._front;\r\n\t\tconst ret = arr[frontIndex];\r\n\t\tarr[frontIndex] = undefined;\r\n\t\tthis._front = (frontIndex \u002B 1) \u0026 (arr.length - 1);\r\n\t\tthis._length -= 1;\r\n\t\treturn ret;\r\n\t}\r\n}\r\n\r\nconst arrayMove = (arr, moveBy, len) =\u003E {\r\n\tfor (let i = 0; i \u003C len; \u002B\u002Bi) {\r\n\t\tarr[i \u002B moveBy] = arr[i];\r\n\t\tarr[i] = undefined;\r\n\t}\r\n};\r\n\r\n\r\nwindow.symbol = Symbol(\u0027symbol\u0027);\r\nwindow.traversalRoot = document.getElementById(\u0027traversal-root\u0027);\r\nfor (const el of traversalRoot.getElementsByTagName(\u0027section\u0027)) {\r\n\tObject.defineProperty(el, window.symbol, { value: {} });\r\n}\r\n\r\nwindow.isMatch = function isMatch(element) {\r\n\treturn element[symbol] !== undefined;\r\n}\r\n\r\nwindow.traverse = function traverse(element, callback) {\r\n\tif (isMatch(element)) {\r\n\t\tcallback(element);\r\n\t} else if (element instanceof HTMLSlotElement \u0026\u0026 element.assignedNodes().length) {\r\n\t\tfor (const child of element.assignedElements()) {\r\n\t\t\ttraverse(child, callback);\r\n\t\t}\r\n\t} else {\r\n\t\tfor (const child of element.children) {\r\n\t\t\ttraverse(child, callback);\r\n\t\t}\r\n\t}\r\n};\r\n\r\nwindow.traverseQueue = function traverseQueue(initialElement, callback) {\r\n\tconst queue = new FastQueue();\r\n\tqueue.push(initialElement);\r\n\r\n\tlet element;\r\n\twhile (element = queue.shift()) {\r\n\t\tif (isMatch(element)) {\r\n callback(element);\r\n } else if (element instanceof HTMLSlotElement \u0026\u0026 element.assignedNodes().length) {\r\n for (const child of element.assignedElements()) {\r\n queue.push(child);\r\n }\r\n } else {\r\n for (const child of element.children) {\r\n queue.push(child);\r\n }\r\n }\r\n\t}\t\r\n};\r\n\r\nwindow.traverseArray = function traverseArray(initialElement, callback) {\r\n\tconst queue = [];\r\n\tqueue.push(initialElement);\r\n\r\n\tlet element;\r\n\twhile (element = queue.shift()) {\r\n\t\tif (isMatch(element)) {\r\n callback(element);\r\n } else if (element instanceof HTMLSlotElement \u0026\u0026 element.assignedNodes().length) {\r\n for (const child of element.assignedElements()) {\r\n queue.push(child);\r\n }\r\n } else {\r\n for (const child of element.children) {\r\n queue.push(child);\r\n }\r\n }\r\n\t}\t\r\n};\r\n","TestCases":[{"Name":"Recursive traversal function","Code":"const results = [];\r\ntraverse(traversalRoot, function callback(element) {\r\n\tfor (const child of element.children) {\r\n\t\ttraverse(child, callback);\r\n\t}\r\n\tresults.push(element);\r\n})","IsDeferred":false},{"Name":"Queue-based traversal function","Code":"const results = [];\r\ntraverseQueue(traversalRoot, function callback(element) {\r\n\tfor (const child of element.children) {\r\n\t\ttraverseQueue(child, callback);\r\n\t}\r\n\tresults.push(element);\r\n})","IsDeferred":false},{"Name":"Array-based traversal function","Code":"const results = [];\r\ntraverseArray(traversalRoot, function callback(element) {\r\n\tfor (const child of element.children) {\r\n\t\ttraverseArray(child, callback);\r\n\t}\r\n\tresults.push(element);\r\n})","IsDeferred":false}]}