Quick union algorithm implemented in JS.