ramnathv / rCharts

Interactive JS Charts from R
http://rcharts.io
Other
1.19k stars 654 forks source link

explore [jsonlite](https://github.com/jeroenooms/JSONlite) for JSON translation #309

Open timelyportfolio opened 10 years ago

timelyportfolio commented 10 years ago

New package jsonlite or CRAN based off RJSONIO looks like a much better and speedier option for JSON translation.

ramnathv commented 10 years ago

I am finding it to be slower.

library(microbenchmark)

microbenchmark(
  jsonlite::toJSON(mtcars),
  RJSONIO::toJSON(mtcars),
  rjson::toJSON(mtcars),
  times = 10
)

Output

Unit: microseconds
                     expr        min         lq      median         uq        max neval
 jsonlite::toJSON(mtcars) 137884.341 142062.506 145352.0815 150982.395 177053.330    10
  RJSONIO::toJSON(mtcars)   1187.170   1280.926   1301.4975   1487.285   2240.849    10
    rjson::toJSON(mtcars)    226.272    230.432    271.4605    275.248    325.430    10
timelyportfolio commented 10 years ago

how odd. I was just downloading. I guess we should await a response on the jsonlite issue. Thanks for checking.

ramnathv commented 10 years ago

Yes. The toJSON function in jsonlite is more flexible and does both rows and columns. It also has a serializeJSON function which is super cool. I am surprised at the performance regression though since it is supposed to be based on RJSONIO.

timelyportfolio commented 10 years ago

I get a very simliar slow result for jsonlite on my Windows machine.

Unit: microseconds
                     expr        min         lq     median         uq      max neval
 jsonlite::toJSON(mtcars) 157140.033 160654.173 161913.867 164999.240 186773.6    10
  RJSONIO::toJSON(mtcars)   1536.035   1548.579   1681.807   1741.676 208468.5    10
    rjson::toJSON(mtcars)    568.648    620.344    633.458    687.244 325837.4    10
timelyportfolio commented 10 years ago

Agree jsonlite is a much more robust and helpful solution, but speed will be a big problem for now.

jeroen commented 10 years ago

Actually only the parser is based on RJSONIO. Both RJSONIO and jsonlite use plain R for toJSON.

timelyportfolio commented 10 years ago

jsonlite seems to be taking the spot as the standard for JSON import/export, and the newest version offers a big speed boost. Just a refresher to consider committing to jsonlite as the converter of choice.

jeroen commented 10 years ago

Just committed another performance tweak. Some benchmarks from the latest dev version:

> microbenchmark(
+   jsonlite::toJSON(diamonds),
+   jsonlite::toJSON(diamonds, dataframe="col"),
+   RJSONIO::toJSON(diamonds),
+   rjson::toJSON(diamonds),
+   times = 10
+ )

Unit: milliseconds
                                          expr      min       lq   median       uq      max neval
                    jsonlite::toJSON(diamonds) 761.1617 766.6734 776.6897 794.7294 810.3345    10
 jsonlite::toJSON(diamonds, dataframe = "col") 317.3538 323.0605 358.2399 369.7034 403.1690    10
                     RJSONIO::toJSON(diamonds) 868.9087 885.8630 893.2560 910.3101 952.7746    10
                       rjson::toJSON(diamonds) 306.6124 309.4605 309.8520 311.4325 324.0248    10

Note that rjson and RJSONIO by default use column based encoding for data frames. Only jsonlite supports row-based encoding. To benchmark performance make sure to compare to jsonlite::toJSON(x, dataframe="columns").

ramnathv commented 10 years ago

This is super-impressive @jeroenooms! I just tested it and the gains are very impressive. I am all for switching to jsonlite. The only thing keeping me from doing it is maintaining Shiny compatibility. I see from the Shiny issues thread that you are already discussing this.