Skip to content

Instantly share code, notes, and snippets.

@jamescook
Last active April 12, 2016 00:57
Show Gist options
  • Save jamescook/16a274c36c4b490a68f9ba54937880da to your computer and use it in GitHub Desktop.
Save jamescook/16a274c36c4b490a68f9ba54937880da to your computer and use it in GitHub Desktop.
require "benchmark/ips"
$LOAD_PATH.push File.expand_path("./lib")
require_relative "./lib/arbre.rb"
if ENV['SECOND_RUN']
class ::Arbre::Element
include BuilderMethods
attr_reader :arbre_context
def initialize(arbre_context = Arbre::Context.new)
@arbre_context = arbre_context
end
def children
@children ||= ElementCollection.new
end
def add_child(child)
return unless child
if child.is_a?(Array)
child.each{|item| add_child(item) }
return children
end
# If its not an element, wrap it in a TextNode
unless child.is_a?(Element)
child = Arbre::HTML::TextNode.from_string(child)
end
if child.respond_to?(:parent)
# Remove the child
child.parent.remove_child(child) if child.parent && child.parent != self
# Set ourselves as the parent
child.parent = self
end
children << child
end
def remove_child(child)
child.parent = nil if child.respond_to?(:parent=)
children.delete(child)
end
def children?
children.any?
end
# Resets the Elements children
def clear_children!
children.clear
end
end
end
CTX = ::Arbre::Context.new
Benchmark.ips do |x|
x.hold! "filename"
x.time = 5
x.warmup = 2
x.report("element w/ ElementCollection") { Arbre::Element.new(CTX) }
x.report("element w/o ElementCollection") { Arbre::Element.new(CTX) }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment