There's a problem with the algorithm itself, which is inefficient. If you try to sort the node, sometimes it gets better results, but it's a heuristic rule, so it's not ensured any improvement.
You can try with:
const root = pack(hierarchy).sort((a, b) => b.value - a.value);