rethinkdb/rethinkdb — pull request #788 by presidentbeef (2013-05-12 13:44:36+00:00)
Title: Proof-of-concept `r.json` method using Oj gem

As discussed in #252, it sure is convenient to pass JSON strings directly to RethinkDB, or at least not have to instantiate all the objects just to convert them to RQL.

So I threw together a slightly different approach to the `r.json` method using the [Oj](http://www.ohler.com/oj/) JSON parser. Sorry, no tests because I can't get RethinkDB to build on my current machine. But some manual testing showed that the basic functionality works, at least.

Here is a quick benchmark using a 660KB JSON file ([code](https://gist.github.com/presidentbeef/724743fd7a11b57cfbf0#file-r_json_bm-rb)):

```
Rehearsal -------------------------------------------------------
JSON.parse + r.expr 8.600000 0.030000 8.630000 ( 8.635941)
Oj.load + r.expr 8.440000 0.010000 8.450000 ( 8.440146)
r.json 0.620000 0.000000 0.620000 ( 0.617726)
r.json stream 0.560000 0.000000 0.560000 ( 0.562764)
--------------------------------------------- total: 18.260000sec

user system total real
JSON.parse + r.expr 7.950000 0.000000 7.950000 ( 7.955163)
Oj.load + r.expr 7.960000 0.000000 7.960000 ( 7.969099)
r.json 0.540000 0.000000 0.540000 ( 0.538718)
r.json stream 0.540000 0.000000 0.540000 ( 0.537156)
```

My conclusion: it's the conversion to RQL that is slow, not the JSON parsing.

I was a little surprised that streaming the file didn't perform better, but it may be that Oj is not actually streaming the data efficiently like I thought it would.