Hacker News new | ask | show | jobs
by hamsterbooster 2205 days ago
This might be a stupid question but how can the site run multiple maze generation algorithm at the same time? My understanding is that javascript is single thread.
2 comments

Really good question actually!

I peeked at the javascript and it looks like when you hit `run`, it is calling the step at a setInterval (if I'm reading this right, as fast as the browser can call run)

So at the end of each run call, the interpreter figures out which setInterval timer to execute next and has the chance to pick other mazes. (I don't know much about how the JS engine resolves which "not quite thread" gets run next when there are multiple candidates)

Thanks! I ran multiple maze at the same time and time it and it seems like the maze ran just as fast as running only one maze at a time. If the interpreter can pick other maze, should it take longer? Did I miss anything here?
The maze generation is slowed down to visualize what is going on. Most of those mazes can be generated in a few of milliseconds.
By using the event loop, with functions like setTimeout