Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions benchmarks/jbuilder_template/array_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@

Post = Struct.new(:id, :body)
json = JbuilderTemplate.new nil
posts = 3.times.map { Post.new(it, "Post ##{it}") }

Benchmark.ips do |x|
x.report('before') do
json.array!(nil)
x.report('before') do |n|
n.times { json.array! }
end
x.report('after') do
json.array!(nil)
x.report('after') do |n|
n.times { json.array! }
end

x.hold! 'temp_array_ips'
Expand All @@ -24,8 +23,8 @@
json = JbuilderTemplate.new nil

Benchmark.memory do |x|
x.report('before') { json.array! posts, :id, :body }
x.report('after') { json.array! posts, :id, :body }
x.report('before') { json.array! }
x.report('after') { json.array! }

x.hold! 'temp_array_memory'
x.compare!
Expand Down
44 changes: 44 additions & 0 deletions benchmarks/jbuilder_template/array_dsl_block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require 'benchmark/ips'
require 'benchmark/memory'
require_relative '../../lib/jbuilder'
require_relative '../../lib/jbuilder/jbuilder_template'

Post = Struct.new(:id, :body)
json = JbuilderTemplate.new nil
array = [1, 2, 3]

Benchmark.ips do |x|
x.report('before') do |n|
n.times do
json.array! array do |item|
end
end
end
x.report('after') do |n|
n.times do
json.array! array do |item|
end
end
end

x.hold! 'temp_array_ips'
x.compare!
end

json = JbuilderTemplate.new nil

Benchmark.memory do |x|
x.report('before') do
json.array! array do |item|
end
end
x.report('after') do
json.array! array do |item|
end
end

x.hold! 'temp_array_memory'
x.compare!
end
45 changes: 45 additions & 0 deletions benchmarks/partials/array_dsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require 'benchmark/ips'
require 'benchmark/memory'
require_relative 'setup'

POST_PARTIAL = <<-JBUILDER
json.extract! post, :id, :body
JBUILDER

PARTIALS = { "_post.json.jbuilder" => POST_PARTIAL }

Post = Struct.new(:id, :body)

view = build_view(fixtures: PARTIALS)
json = JbuilderTemplate.new view
posts = 3.times.map { Post.new(it, "Post ##{it}") }

Benchmark.ips do |x|
x.report('before') do |n|
n.times { json.array! posts, partial: "post", as: :post }
end

x.report('after') do |n|
n.times { json.array! posts, partial: "post", as: :post }
end

x.hold! 'temp_array_results_ips'
x.compare!
end

json = JbuilderTemplate.new view

Benchmark.memory do |x|
x.report('before') do
json.array! posts, partial: "post", as: :post
end

x.report('after') do
json.array! posts, partial: "post", as: :post
end

x.hold! 'temp_array_results_memory'
x.compare!
end
6 changes: 4 additions & 2 deletions lib/jbuilder/jbuilder_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,16 @@ def target!
@cached_root || super
end

def array!(collection = EMPTY_ARRAY, *args, &block)
def array!(collection = EMPTY_ARRAY, *args)
options = args.first

if _partial_options?(options)
options[:collection] = collection
_render_partial_with_options options
Comment on lines 124 to 126
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I should also add a benchmark for array! partial:...

elsif ::Kernel.block_given?
_array(collection, args) { |x| yield x }
else
_array collection, args, &block
_array collection, args
end
end

Expand Down