Closed iggant closed 8 years ago
If you can be absolutely sure the array contains only strings, numbers and booleans, I guess it would work. More of an app-specific hack though, I don't think it's appropriate for this repository.
As @nateberkopec said, it's app-specific. In some cases it might be OK, in most - just not. This is definitely out of scope of this repo. If you need faster JSON serializer, you can use oj:
require "benchmark/ips"
require "oj"
PAYLOAD = ['2', 4, 8, 4.8, 'yuunnnd', 89, 'rrrrrr', 'fsdfsdafasdfasdfasdfas']
Benchmark.ips do |x|
x.report("inspect") { PAYLOAD.inspect }
x.report("oj.dump") { Oj.dump PAYLOAD }
x.compare!
end
the above will produce:
Calculating -------------------------------------
inspect 19.877k i/100ms
oj.dump 48.248k i/100ms
-------------------------------------------------
inspect 267.444k (± 3.2%) i/s - 1.352M
oj.dump 804.960k (± 6.1%) i/s - 4.005M
Comparison:
oj.dump: 804959.9 i/s
inspect: 267443.6 i/s - 3.01x slower
Also, there are in fact a lot more factors which can affect speed of to_json
.
If in array contain just strings can I use inspect instead of to_json
Can I? Using inspect looks like unnatural but very fast, can some one explain about cons.