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.

12 Responses to “actionjson – The fastest ActionScript 3.0 JSON library”

  1. actionjson最快的JSON解析器 « asblog Says:

    [...] http://blog.brokenfunction.com/2010/10/30/actionjson-the-fastest-actionscript-3-0-json-parser/ 评论 [...]

  2. Vic Says:

    Hey, thx for the code. I’ll try it.
    Do you … have twitter so I can follow you?

    puppetMaster3 (on twitter

  3. Jozef Chutka Says:

    Good job there. thinking of comparing processing speed with http://www.blooddy.by/en/crypto/ lib

  4. makc Says:

    Have you tested against http://www.blooddy.by/en/crypto/benchmark/ ?

  5. Brett Says:

    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’ ?

  6. Kevin Says:

    Have you thought about merging this into corelib? It’s on github now too.

    https://github.com/mikechambers/as3corelib

  7. Max Says:

    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.

  8. BlooDHounD Says:

    http://www.blooddy.by/en/benchmark

  9. Sharron Clemons Says:

    Have you tested against http://www.blooddy.by/en/crypto/benchmark/ ?

  10. Neon Light Says:

    ;*, I am very thankful to this topic because it really gives great information “`-

  11. Introducing CoThread: Easily write threads in AS3/Flash/Flex/AIR | Taytay.com Says:

    [...] I stumbled across BrokenFunction’s json code. He had an example of an async json parser. It was recursive, but asynchronous. It could pause [...]

  12. Simon Says:

    Can anyone provide an example code of how to use encodeJson and decodeJson? Thanks

Leave a Reply