From 1b32713581588ef2efe13dbef442ffacec055db0 Mon Sep 17 00:00:00 2001 From: preciz Date: Thu, 9 Apr 2026 19:03:34 +0200 Subject: [PATCH] Optimize multiple selectors deduping Use `Enum.uniq_by/2` with the `node_id` integer instead of relying on `Enum.uniq/1`'s deep structural equality over massive `HTMLNode` structs. Moving the de-duplication before the sort also results in less elements to be sorted, reducing the list that goes into the O(N log N) sort. --- lib/floki/finder.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/floki/finder.ex b/lib/floki/finder.ex index eaa1b26a..09cd119f 100644 --- a/lib/floki/finder.ex +++ b/lib/floki/finder.ex @@ -76,8 +76,8 @@ defmodule Floki.Finder do def find(%HTMLTree{} = tree, selectors) when is_list(selectors) do Enum.flat_map(selectors, fn s -> traverse_html_tree(tree.node_ids, s, tree, []) end) + |> Enum.uniq_by(& &1.node_id) |> Enum.sort_by(& &1.node_id) - |> Enum.uniq() end # some selectors can be applied with the raw html tree tuples instead of