Is simple serialization faster than JSON? (in Ruby)

I have an application written in ruby ​​(which works in JRuby VM). By profiling it, I realized that it spends a lot (almost almost all) of its time converting some hashes into JSON.

These hashes have character keys, values ​​for other similar hashes, arrays, strings, and numbers.

Is there a serialization method suitable for such input and usually works faster than JSON? It would be preferable if he had a Java or JRuby-compatible gem.

I am currently using jruby-jsongem, which is the fastest implementation JSONin JRuby (as I was told), so the transition will most likely be to a different serialization method, and not just to another library.

Any help is appreciated! Thank.

+3
source share
3 answers

I just heard about this project 20 minutes ago (published in hacker news), it has a Ruby implementation: http://msgpack.sourceforge.net/#GettingStarted

MessagePack is an efficient binary code library for serializing objects. It allows you to exchange structured objects between many languages, such as JSON. But unlike JSON, it is very fast and small.

+6
source

I don’t know how much the performance of JRuby differs, but in MRI I find it Marshalusually faster.

, , , 3 , JSON ( , YAML), 2x (. JSON).

, - , . Benchmark? - :

require 'benchmark'
# other requires as necessary
N = 100 # or whatever multiple is needed to get a sensible result
Benchmark.bm(20) do |rpt|
  rpt.report("YAML") do
    N.times do
      # perform your task using YAML
    end
  end
  rpt.report("JSON") do
    # as above for JSON
  end
  rpt.report("Marshal") do
    # as above for JSON
  end
end
+3

(): , , , . , YAJL Java JRuby. , YAJL , .)

JSON ( , ), YAJL. Ruby: YAJL-Ruby.

, JSON- Ruby.

+3

Source: https://habr.com/ru/post/1737652/


All Articles