The insert code looks fine to me, assuming children of XML are stored as properties "0", "1", "2", ... It's shifting all children with index >= i and saving P starting at child index i.
[[Put]] is overwriting a child at a given index, and insertChildBefore/insertChildAfter/prependChild should be doing the same type of operation as insert.