Modify node traversal loop to avoid JS roots assertion. Fixes #2321.