Improved HTML void element detection performance.

This ensures we only call String#downcase if we can't find an all lowercased
*and* all uppercased version of the element name. This in turn can save as many
object allocations as there are HTML opening tags.

This fixes #52.
This commit is contained in:
Yorick Peterse 2014-09-24 11:07:34 +02:00
parent c89ac91f3a
commit 4469ffc5b1
2 changed files with 8 additions and 1 deletions

View File

@ -24,5 +24,7 @@ module Oga
'track', 'track',
'wbr' 'wbr'
]) ])
HTML_VOID_ELEMENTS.merge(HTML_VOID_ELEMENTS.map { |name| name.upcase })
end # XML end # XML
end # Oga end # Oga

View File

@ -322,7 +322,12 @@ module Oga
# Called on the closing `>` of the open tag of an element. # Called on the closing `>` of the open tag of an element.
# #
def on_element_open_end def on_element_open_end
if html? and HTML_VOID_ELEMENTS.include?(current_element.downcase) return unless html?
# Only downcase the name if we can't find an all lower/upper version of
# the element name. This can save us a *lot* of String allocations.
if HTML_VOID_ELEMENTS.include?(current_element) \
or HTML_VOID_ELEMENTS.include?(current_element.downcase)
add_token(:T_ELEM_END) add_token(:T_ELEM_END)
@elements.pop @elements.pop
end end