3 ms·
I'm torn. On one hand, yes, this is unsatisfactorily slow. On the other, all my experience has taught me that making objects (especially in the case of ActiveR
by threedaymonk 14y ago
I'm torn. On one hand, yes, this is unsatisfactorily slow.
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!
- kranzky 14y agoSure, we do, in fact, use RABL (https://github.com/nesquena/rabl https://github.com/nesquena/rabl) for much of our JSONifying needs. In general a much better solution than calling .to_json on objects directly. 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.
- Breefield 14y agoRABL caching is still not as robust as it needs to be though, it's a shame. I use OJ to speed up the actual JSON render: https://github.com/ohler55/oj https://github.com/ohler55/oj