Skip to content

Instantly share code, notes, and snippets.

@dirk
Created October 6, 2015 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirk/4048df31641f5dab1c67 to your computer and use it in GitHub Desktop.
Save dirk/4048df31641f5dab1c67 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'benchmark/ips'
require 'ruby-prof'
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'lib')
require 'jbuilder'
require 'jbuilder/jbuilder_template'
module Rails
def self.cache
@cache ||= ActiveSupport::Cache::MemoryStore.new
end
end
class FakeController
def perform_caching
true
end
end
class FakeContext
attr_reader :controller
def initialize
@controller = FakeController.new
end
end
def render
JbuilderTemplate.encode FakeContext.new do |json|
yield json
end
end
def keys json, i
json.a i
json.b i
json.c i
json.d i
json.e i
json.subitems 0..100 do |j|
json.f i.to_s * j
json.g i.to_s * j
json.h i.to_s * j
json.i i.to_s * j
json.j i.to_s * j
end
end
Benchmark.ips do |x|
x.report 'basic' do
render do |json|
# Replicating the large arrays with objects-with-lots-of-keys scenario
json.cached do
json.cache! 'cached' do
json.array! 0..100 do |i|
keys json, i
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment