Jackson data binding: which is faster, method (geter/setter) or field access?
After adding the ability to use direct field access for data binding for Jackson 1.1 -- in addition to already existing setter/getter-based access -- (see the previous entry), I started wondering whether there might be significant performance differences between two methods. Why? Because an earlier micro-benchmark from a while back suggested that there might just be such a difference, in favor of method-based access.
But is there? I modified code under 'src/perf' a bit (classes TestSerPerf, TestDeserPerf), and here's a sample from output from running both micro benchmarks.
Serialization:
Test 'Jackson, object+GET, final' -> 51 msecs (236). Test 'Jackson, object+Field, final' -> 51 msecs (8). Test 'Jackson, tree' -> 49 msecs (36).
Deserialization:
Test 'Jackson, object+SET' -> 72 msecs (54). Test 'Jackson, object+Field' -> 69 msecs (33). Test 'Jackson, tree' -> 80 msecs (122).
("tree" variant gives perspective on how different is speed of serializing/deserializer JsonNode - based TreeModels)
So the short answer is: no, there is not much difference (actual values
fluctuate during the run, but the relative ratios are rather constant).
This is quite different from the earlier results; and either JDK
Reflection access for Fields has improved a lot with 1.6 updates; or
most of the time is spent in places different from actual direct access
(quite plausible).
So fortunately you can choose access method you
prefer based on factors other than one of them being significantly
slower than the other. :-)
Also, on a related note, it is interesting to note that it is slightly faster to serialize Tree Models (write json from in-memory tree) than from POJOs, but conversely bit slower. Latter is probably because building LinkedHashMaps is slower than constructing actual beans.