child.tagName will always be uppercase, so if you just do tag=tag.toUpperCase() at the top-level, then you can do a simpler comparison here without calling any functions.