actionjson – The fastest ActionScript 3.0 JSON library
October 30th, 2010
Adobe’s as3corelib has a JSON parser. It’s quite stable, and widely used. It’s also dirt slow, and has a hard time getting through large amounts of data. Other libraries are either slower, or prohibitively licensed.
So, I set out to write the best AS3 JSON library I possibly could. I’m calling it actionjson.
encodeJson and decodeJson
Using pretty much every trick I could think of, I wrote a new blocking JSON encoder and decoder, much like the as3corelib version, but only capable of processing much faster. They’re also single, isolated functions, keeping them light and unbound from any extra dependencies.
On large objects the blocking decoder performs up to 5-6x faster. The JSON encoder has more modest improvements, since the as3corelib version is already reasonably optimized, but it’s still significantly faster at handling strings and objects. It’s 2-3x faster at them them in my tests.
ason was another JSON library that looked promising (if not for its license) that I wanted to compare actionjson against, but in my tests it seemed to have some severe performance problems. The decoder was around 10-20x slower than as3corelib’s decoder. The encoder was much better, even performing better than encodeJson in a few tests, but still performing 2-3x worse on objects, arrays, and long strings.
JsonDecoderAsync
I also wrote an asynchronous JSON decoder that can parse data incrementally, either as the data comes in, or in specified chunks. It has it’s own stack, which adds a lot of overhead, but still ends up around 2x faster than the one in as3corelib.
There is no asynchronous JSON encoder included, mostly because I don’t think it’s paticularly needed at this point, but I think it would be a good addition to the library in the future.
Wasted cycles
The advantages largely come from avoiding overhead from classes, constants (yes, constants), switch and if statements, and by analyzing using ByteArrays instead of Strings.
I actually tried using Apparat in my project, specifically tdsi so i could use Flash’s direct memory access opcodes. Unfortunately it performed at about half the speed, although I’m not sure why.
Download
actionjson is available on github here. It’s licensed under the Apache License 2.0, so it’s usable in proprietary and open-source projects. The library includes the (simple) unit tests I used to verify my code, as well as the speed tests.
Feedback and contributions are very welcome. Leave comments here, email me, or fork me on github.
October 31st, 2010 at 6:52 am
[…] http://blog.brokenfunction.com/2010/10/30/actionjson-the-fastest-actionscript-3-0-json-parser/ 评论 […]
October 31st, 2010 at 11:57 am
Hey, thx for the code. I’ll try it.
Do you … have twitter so I can follow you?
puppetMaster3 (on twitter
October 31st, 2010 at 6:07 pm
Good job there. thinking of comparing processing speed with http://www.blooddy.by/en/crypto/ lib
October 31st, 2010 at 7:19 pm
Have you tested against http://www.blooddy.by/en/crypto/benchmark/ ?
November 1st, 2010 at 5:26 am
that’s great man ! what about getting Typed object back 😉 any thought on implementing an actionjsonVo extension ? or do you just map your Object to the Vo ‘manually’ ?
November 4th, 2010 at 4:55 pm
Have you thought about merging this into corelib? It’s on github now too.
https://github.com/mikechambers/as3corelib
November 11th, 2010 at 2:49 am
Vic: @pandertome
Brett: I’m thinking about it. If I did it, I’d have to do it in a way I felt would work well with Flash and take advantage of static typing, but I’m not sure there’s a good way to do that. So, maybe.
Kevin: I’d love to merge it in, but there are some differences between the libraries, so to do it they’d probably have to have both json libraries in as3corelib, which is a little odd.
We also have different licenses, although I think they might be compatible anyway.
November 28th, 2010 at 1:56 pm
http://www.blooddy.by/en/benchmark
December 21st, 2010 at 8:20 pm
Have you tested against http://www.blooddy.by/en/crypto/benchmark/ ?
January 28th, 2011 at 4:07 am
;*, I am very thankful to this topic because it really gives great information “`-
January 29th, 2012 at 7:59 pm
[…] I stumbled across BrokenFunction’s json code. He had an example of an async json parser. It was recursive, but asynchronous. It could pause […]
March 12th, 2012 at 12:20 pm
Can anyone provide an example code of how to use encodeJson and decodeJson? Thanks