-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Match DeferredRender in performance (#3)
- Loading branch information
Showing
4 changed files
with
172 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ gemspec | |
gem "rake", "~> 13.0" | ||
gem "minitest", "~> 5.16" | ||
gem "standard", "~> 1.3" | ||
gem "benchmark-ips" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
require "benchmark" | ||
require "benchmark/ips" | ||
require_relative "../lib/phlex/slotable" | ||
|
||
class DeferredList < Phlex::HTML | ||
include Phlex::DeferredRender | ||
|
||
def initialize | ||
@items = [] | ||
end | ||
|
||
def template | ||
if @header | ||
h1(class: "header", &@header) | ||
end | ||
|
||
ul do | ||
@items.each do |item| | ||
li { render(item) } | ||
end | ||
end | ||
end | ||
|
||
def header(&block) | ||
@header = block | ||
end | ||
|
||
def with_item(&content) | ||
@items << content | ||
end | ||
end | ||
|
||
class SlotableList < Phlex::HTML | ||
include Phlex::Slotable | ||
|
||
slot :header | ||
slot :item, many: true | ||
|
||
def template | ||
if header_slot | ||
h1(class: "header", &header_slot) | ||
end | ||
|
||
ul do | ||
item_slots.each do |slot| | ||
li { render(slot) } | ||
end | ||
end | ||
end | ||
end | ||
|
||
class DeferredListExample < Phlex::HTML | ||
def template | ||
render DeferredList.new do |list| | ||
list.header do | ||
"Header" | ||
end | ||
|
||
list.with_item do | ||
"One" | ||
end | ||
|
||
list.with_item do | ||
"two" | ||
end | ||
end | ||
end | ||
end | ||
|
||
class SlotableListExample < Phlex::HTML | ||
def template | ||
render SlotableList.new do |list| | ||
list.with_header do | ||
"Header" | ||
end | ||
|
||
list.with_item do | ||
"One" | ||
end | ||
|
||
list.with_item do | ||
"two" | ||
end | ||
end | ||
end | ||
end | ||
|
||
puts RUBY_DESCRIPTION | ||
|
||
deferred_list = DeferredListExample.new.call | ||
slotable_list = SlotableListExample.new.call | ||
|
||
raise unless deferred_list == slotable_list | ||
|
||
Benchmark.bmbm do |x| | ||
x.report("Deferred") { 1_000_000.times { DeferredListExample.new.call } } | ||
x.report("Slotable") { 1_000_000.times { SlotableListExample.new.call } } | ||
end | ||
|
||
puts | ||
|
||
Benchmark.ips do |x| | ||
x.report("Deferred") { DeferredListExample.new.call } | ||
x.report("Slotable") { SlotableListExample.new.call } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters