Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Node#wrap should accept a Node #2657

Closed
flavorjones opened this issue Oct 3, 2022 · 0 comments · Fixed by #2699
Closed

[feature] Node#wrap should accept a Node #2657

flavorjones opened this issue Oct 3, 2022 · 0 comments · Fixed by #2699

Comments

@flavorjones
Copy link
Member

What problem are you trying to solve?

Currently Node#wrap only accepts a string which is parsed as a fragment. For some use cases where many nodes are being wrapped, this is about 2x slower than simply using an existing Node object.

I think Node#wrap should be able to accept a Node argument, which is duplicated for use instead of forcing callers to pass a String and suffer the performance penalty of invoking the parser.

Please show your code!

Benchmarks:

#! /usr/bin/env ruby

require "nokogiri"
require "benchmark/ips"

HTML = "<body><section>" + ("<span>foo</span>" * 100) + "</section></body>"

def dup_reparent
  doc = Nokogiri::HTML::Document.parse(HTML)
  node = doc.create_element("p")
  doc.at_css("section").children.each do |child|
    new_node = node.dup
    child.parent.add_child(new_node)
    new_node.add_child(child)
  end
  doc
end

def wrap
  doc = Nokogiri::HTML::Document.parse(HTML)
  doc.at_css("section").children.each do |child|
    child.wrap("<p>")
  end
  doc
end

Benchmark.ips do |x|
  x.report("dup-reparent") do
    dup_reparent
  end

  x.report("wrap") do
    wrap
  end

  x.compare!
end
Warming up --------------------------------------
        dup-reparent   230.000  i/100ms
                wrap   108.000  i/100ms
Calculating -------------------------------------
        dup-reparent      2.311k (± 4.8%) i/s -     11.730k in   5.088169s
                wrap      1.099k (± 7.0%) i/s -      5.508k in   5.040604s

Comparison:
        dup-reparent:     2310.8 i/s
                wrap:     1099.2 i/s - 2.10x  (± 0.00) slower
@flavorjones flavorjones added this to the v1.14.0 milestone Nov 14, 2022
flavorjones added a commit that referenced this issue Nov 14, 2022
Duplicating an instantiated Node is significantly faster than
re-parsing a string for multiple invocations.

Closes #2657
flavorjones added a commit that referenced this issue Nov 15, 2022
Duplicating an instantiated Node is significantly faster than
re-parsing a string for multiple invocations.

Closes #2657
flavorjones added a commit that referenced this issue Nov 15, 2022
Duplicating an instantiated Node is significantly faster than
re-parsing a string for multiple invocations.

Closes #2657
flavorjones added a commit that referenced this issue Nov 15, 2022
Duplicating an instantiated Node is significantly faster than
re-parsing a string for multiple invocations.

Closes #2657
flavorjones added a commit that referenced this issue Nov 15, 2022
Duplicating an instantiated Node is significantly faster than
re-parsing a string for multiple invocations.

Closes #2657
flavorjones added a commit that referenced this issue Nov 16, 2022
Duplicating an instantiated Node is significantly faster than
re-parsing a string for multiple invocations.

Note that we now also explicitly use the node's `parent` as the
context node for parsing markup, if it exists.

Closes #2657
flavorjones added a commit that referenced this issue Nov 16, 2022
Duplicating an instantiated Node is significantly faster than
re-parsing a string for multiple invocations.

Note that we now also explicitly use the node's `parent` as the
context node for parsing markup, if it exists.

Closes #2657
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant