Created
December 30, 2024 21:14
-
-
Save omegahm/9bfbb068a60cdd04817e9c8d680ab834 to your computer and use it in GitHub Desktop.
Benchmarking var vs. attr vs. def
This file contains hidden or 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
require "benchmark/ips" | |
class VarComponent | |
def initialize(klass = nil) | |
@class = klass | |
@size = "w-48 h-64" | |
@shadow = "shadow-md" | |
@hover = "hover:bg-base-300 hover:scale-105 transition-transform" | |
@structure = "flex flex-col items-start rounded-lg" | |
@spacing = "gap-y-4 p-4" | |
@color = "bg-base-200" | |
end | |
def view_template | |
[@structure, @color, @hover, @size, @shadow, @spacing, @class] | |
end | |
end | |
class AttrComponent | |
attr_reader :size, :shadow, :hover, :structure, :spacing, :color | |
def initialize(klass = nil) | |
@class = klass | |
@size = "w-48 h-64" | |
@shadow = "shadow-md" | |
@hover = "hover:bg-base-300 hover:scale-105 transition-transform" | |
@structure = "flex flex-col items-start rounded-lg" | |
@spacing = "gap-y-4 p-4" | |
@color = "bg-base-200" | |
end | |
def view_template | |
[structure, color, hover, size, shadow, spacing, @class] | |
end | |
end | |
class DefComponent | |
def initialize(klass = nil) | |
@class = klass | |
end | |
def size = "w-48 h-64" | |
def shadow = "shadow-md" | |
def hover = "hover:bg-base-300 hover:scale-105 transition-transform" | |
def structure = "flex flex-col items-start rounded-lg" | |
def spacing = "gap-y-4 p-4" | |
def color = "bg-base-200" | |
def view_template | |
[structure, color, hover, size, shadow, spacing, @class] | |
end | |
end | |
Benchmark.ips do |x| | |
x.report("VarComponent") { VarComponent.new.view_template } | |
x.report("AttrComponent") { AttrComponent.new.view_template } | |
x.report("DefComponent") { DefComponent.new.view_template } | |
x.compare! | |
end |
This file contains hidden or 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
$ ruby --yjit attr_vs_def.rb | |
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +YJIT +PRISM [arm64-darwin24] | |
Warming up -------------------------------------- | |
VarComponent 327.614k i/100ms | |
AttrComponent 327.468k i/100ms | |
DefComponent 336.047k i/100ms | |
Calculating ------------------------------------- | |
VarComponent 3.525M (± 0.8%) i/s (283.73 ns/i) - 17.691M in 5.019741s | |
AttrComponent 3.488M (± 0.5%) i/s (286.69 ns/i) - 17.683M in 5.069648s | |
DefComponent 3.550M (± 2.4%) i/s (281.66 ns/i) - 17.810M in 5.019447s | |
Comparison: | |
DefComponent: 3550409.6 i/s | |
VarComponent: 3524537.6 i/s - same-ish: difference falls within error | |
AttrComponent: 3488137.8 i/s - same-ish: difference falls within error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment