The performance of .to_json in Rails sucks
devblog.agworld.com.au
devblog.agworld.com.au
On the other, all my experience has taught me that making objects (especially in the case of ActiveRecord objects that are mapped to database tables) directly responsible for their API serialisation leads to pain: those objects end up bloated, the API reflects internal concerns rather than usage patterns, and it's really hard to evolve the API or to provide multiple versions simultaneously.
So even though .to_json sucks, I'm far from convinced that it matters. I'd fix it by taking it out entirely!
However, we also have situations where we have a reasonably large Hash that has a very trivial structure. In that case we do call .to_json directly on the Hash. And those are the situations where we're seeing a big performance hit.