logoalt Hacker News

tubstoday at 9:55 AM2 repliesview on HN

Why create an array each time it iterates a Set rather than just iterating the Set?


Replies

willybraunertoday at 9:57 AM

I create a new copy of the Set because the functions called during the loop might modify it by removing elements via cleanup functions. Without a copy, iterating directly on the Set could cause unpredictable behavior.

willybraunertoday at 9:59 AM

But as the article points out, this is a naive implementation. It could be discussed and greatly improved.