Ruby-on-rails 在rails中渲染json的最快方法是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10451722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the fastest way to render json in rails
提问by John Naegle
I'm optimizing some slow transactions in our Rails application and a I see significant time spent rendering JSON views:
我正在优化 Rails 应用程序中的一些慢速事务,并且发现渲染 JSON 视图花费了大量时间:
Rendered welcome/index.json.rabl (490.5ms)
Completed 200 OK in 1174ms (Views: 479.6ms | ActiveRecord: 27.8ms)
Assuming that the API call is returning exactly the data it needs to return, What is the fastest way to render JSON in rails?
假设 API 调用返回的正是它需要返回的数据,那么在 Rails 中呈现 JSON 的最快方法是什么?
We are using Rablbecause of the ability to share code easily, but we aren't tied to it.
我们使用Rabl是因为它能够轻松共享代码,但我们并不依赖于它。
采纳答案by Ezekiel Templin
Rabl uses multi_jsonfor compatibility across platforms and doesn't use the quite fast Yajl library by default. Rabl's config documentationexplains the solution:
Rablmulti_json用于跨平台兼容性,默认情况下不使用相当快的 Yajl 库。Rabl 的配置文档解释了解决方案:
# Gemfile
gem 'yajl-ruby', :require => "yajl"
In the event that still isn't performant enough, you might want to explore a different JSON serializer like oj. You could also instrumentyour render and see where the bottleneck exists.
回答by Gavin Brock
Currently ojseems to be the fastest renderer - beating yajl (according to the oj author's comparison).
目前oj似乎是最快的渲染器 - 击败 yajl(根据 oj 作者的比较)。
Oj is used by default in the latest multi_json (and rails uses mutli_json by default), so swapping to oj should be as simple as adding the following to your Gemfile:
在最新的 multi_json 中默认使用 Oj(并且 rails 默认使用 mutli_json),所以交换到 oj 应该就像在你的 Gemfile 中添加以下内容一样简单:
# Gemfile
gem "oj"
Then each time you call render, it will now use oj.
然后每次调用 render 时,它现在都会使用 oj。
render :json => { ... } # uses multi_json which uses oj
Oj also provides additional specific interfaces, if you want even more performance, but sticking to multi_json makes it easier to swap out gems in the future.
Oj 还提供了额外的特定接口,如果您想要更高的性能,但坚持使用 multi_json 可以更容易地在未来更换 gem。
Note that if you have any { ... }.to_jsoncalls - these will not be upgraded to use oj unless you call Oj.mimic_JSONin an initializer.
请注意,如果您有任何{ ... }.to_json调用 - 除非您调用Oj.mimic_JSON初始化程序,否则这些调用不会升级为使用 oj 。
回答by lulalala
Rails 3 uses multi_json, but it only uses it for json decoding, not encoding. Json encoding/rendering/generation uses ActiveSupport JSON library's to_jsonmethod, therefore is always slow (even if you uses Oj gem).
Rails 3 使用 multi_json,但它只用于json 解码,而不是 encoding。Json 编码/渲染/生成使用 ActiveSupport JSON 库的to_json方法,因此总是很慢(即使您使用 Oj gem)。
You can explicitly rendering using multi_json by doing:
您可以通过执行以下操作使用 multi_json 显式渲染:
render :json => MultiJson.dump(@posts)
Or you can try rails-patch-json-encode gem(by me) which will use multi_json by default. It will affect all build-in to_jsonmethods, so make sure all the tests passes.
或者您可以尝试rails-patch-json-encode gem(由我提供),默认情况下将使用 multi_json。它会影响所有内置to_json方法,因此请确保所有测试都通过。
回答by David Krider
Netflix recently released a new JSON rendering library which is supposedly 25-40 times faster than the default library. Announcement. Code. You'll need to create a new Serializer to take advantage of it, but for people who are impacted, that doesn't seem to be a big hurdle.
Netflix 最近发布了一个新的 JSON 渲染库,据说它比默认库快 25-40 倍。公告。代码。您需要创建一个新的 Serializer 来利用它,但对于受到影响的人来说,这似乎不是一个大障碍。

