diff -r 61bd6974405f Lib/xml/dom/minidom.py --- a/Lib/xml/dom/minidom.py Sat Jun 18 04:18:24 2016 +0300 +++ b/Lib/xml/dom/minidom.py Sat Jun 18 09:38:41 2016 +0300 @@ -78,6 +78,19 @@ class Node(xml.dom.Node): if self.childNodes: return self.childNodes[-1] + def _index(self, refChild): + childNodes = self.childNodes + try: + index = self._index_cache + if childNodes[index] == refChild: + return index + except (AttributeError, IndexError): + pass + try: + return childNodes.index(refChild) + except ValueError: + raise xml.dom.NotFoundErr() + def insertBefore(self, newChild, refChild): if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE: for c in tuple(newChild.childNodes): @@ -92,10 +105,8 @@ class Node(xml.dom.Node): if refChild is None: self.appendChild(newChild) else: - try: - index = self.childNodes.index(refChild) - except ValueError: - raise xml.dom.NotFoundErr() + index = self._index(refChild) + self._index_cache = index + 1 if newChild.nodeType in _nodeTypes_with_children: _clear_id_cache(self) self.childNodes.insert(index, newChild) @@ -139,10 +150,7 @@ class Node(xml.dom.Node): return if newChild.parentNode is not None: newChild.parentNode.removeChild(newChild) - try: - index = self.childNodes.index(oldChild) - except ValueError: - raise xml.dom.NotFoundErr() + self._index_cache = index = self._index(oldChild) self.childNodes[index] = newChild newChild.parentNode = self oldChild.parentNode = None @@ -661,7 +669,7 @@ class TypeInfo(object): class Element(Node): __slots__=('ownerDocument', 'parentNode', 'tagName', 'nodeName', 'prefix', 'namespaceURI', '_localName', 'childNodes', '_attrs', '_attrsNS', - 'nextSibling', 'previousSibling') + 'nextSibling', 'previousSibling', '_index_cache') nodeType = Node.ELEMENT_NODE nodeValue = None schemaType = _no_type