In the previous post, I gave an example of Coding/Decoding an enum with
associated values using a helper struct that mirrored the actual contents of the
JSON being parsed.
Here’s an example of doing the same thing, but directly impementing
init(from decoder:) and encode(to encoder:), instead of using the helper struct.
Seems cleaner, right? The only snag is that when I’ve tried the two approaches with
my real-world data, I’ve found that this direct approach is noticeably slower.
When decoding an array of 1000 enums, the helper struct version takes about 0.630s
and the direct decoding version takes about 0.733s. The effect seems to scale fairly
linearly (10000 takes 6.3s/7.3s etc.). Where’s that extra 15% of the time going?
I don’t know.