Yeah, I guess let's just have protection against the case that childNode somehow comes after `index` already, which will cause an infinite loop the way you have it written.

(Side note: I found `beforeNode` to be a confusing name; I interpreted it as "insert childNode before beforeNode", not "beforeNode should appear before childNode". Not sure what would be clearer though.)