From 18a2cf824d08ecef752ba0e4902b745f3eed9683 Mon Sep 17 00:00:00 2001 From: Yuri Pereira Constante Date: Thu, 4 Jan 2024 12:47:54 -0300 Subject: [PATCH] Optimize id matching (#519) * Optimize id matching * Use pattern matching to get id value * Check equality in the pattern match --- lib/floki/selector.ex | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/floki/selector.ex b/lib/floki/selector.ex index b40a96ca..42290d1e 100644 --- a/lib/floki/selector.ex +++ b/lib/floki/selector.ex @@ -103,15 +103,15 @@ defmodule Floki.Selector do defp id_match?(%HTMLNode{attributes: []}, _), do: false defp id_match?(%HTMLNode{type: :pi}, _), do: false - defp id_match?(%HTMLNode{attributes: attributes}, id) do - Enum.any?(attributes, fn attribute -> - case attribute do - {"id", ^id} -> true - _ -> false - end - end) + defp id_match?(%HTMLNode{attributes: attributes}, id) when is_list(attributes) do + id_attr_value = :proplists.get_value("id", attributes, nil) + id_attr_value == id end + defp id_match?(%HTMLNode{attributes: %{"id" => id}}, id), do: true + + defp id_match?(_node, _id), do: false + defp namespace_match?(_node, namespace) when is_wildcard(namespace), do: true defp namespace_match?(%HTMLNode{type: :pi}, _type), do: false