Working in rails 3.2.1, I am bilding an application which performs a large number of JSON requests and parses the returned data using Yajl. My dilemma is weather or not to initialize a Yajl parser object each time a JSON data is requested:
json = StringIO.new( some_jason_object)
#hmm I need something to parse this json data
parser = Yajl::Parser.new
hash = parser.parse(json)
or to initialize the parser a global constant PARSER = Yajl::Parser.new
in config/initializers/yajl_parser.rb
and call it from my application as follows:
json = StringIO.new( some_jason_object)
hash = PARSER.parse(json)
Given that requests requiring Yajl to parse data will be made on the majority of page request, which implementation offers the best performance.