在Rails中渲染JSON的最快方法是什么 [英] What is the fastest way to render json in rails

查看:116
本文介绍了在Rails中渲染JSON的最快方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在优化我们的Rails应用程序中的一些缓慢的事务,并且看到花费大量时间来渲染JSON视图:

I'm optimizing some slow transactions in our Rails application and a I see significant time spent rendering JSON views:

Rendered welcome/index.json.rabl (490.5ms)
Completed 200 OK in 1174ms (Views: 479.6ms | ActiveRecord: 27.8ms)

假设API调用正好返回了需要返回的数据,在Rails中呈现JSON的最快方法是什么?

Assuming that the API call is returning exactly the data it needs to return, What is the fastest way to render JSON in rails?

我们正在使用 Rabl ,因为它能够轻松共享代码,但我们并不局限于此它.

We are using Rabl because of the ability to share code easily, but we aren't tied to it.

推荐答案

Rabl使用multi_json来实现跨平台的兼容性,并且默认情况下不使用速度非常快的Yajl库. Rabl的配置文档解释了解决方案:

Rabl uses multi_json for compatibility across platforms and doesn't use the quite fast Yajl library by default. Rabl's config documentation explains the solution:

# Gemfile
gem 'yajl-ruby', :require => "yajl"

如果仍然无法提供足够的性能,则可能需要探索其他 JSON序列化程序,例如 oj .您可以也可以对进行渲染,并查看瓶颈所在的位置.

In the event that still isn't performant enough, you might want to explore a different JSON serializer like oj. You could also instrument your render and see where the bottleneck exists.

这篇关于在Rails中渲染JSON的最快方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆