2014 UPDATE
I have updated this post here: Benchmark of node.js JSON Validation Modules Part 3
Old Post
I need to validate JSON in a node.js project I am working on.
Speed is critical in my project, so I decided to benchmark several node.js JSON validation modules.
I only included modules that support the JSON schema described here: http://json-schema.org/
The following modules were benchmarked:
- https://github.com/kriszyp/json-schema
- https://github.com/akidee/schema.js
- https://github.com/garycourt/JSV
Two JSON objects and schemas were used, one basic and one advanced. The source code for these are at the bottom of this post.
Due to varying module support, I had to create both a v2 and v3 schema document.
Results
Conclusion
Looks like JSV is the slowest, even more so when dealing with V3 of the JSON schema. Also note that we are using a pre-processed JSV JSON schema. When that wasn't used, JSV was an additional 20 times slower.
So easy conclusion right? Use json-schema or schema as they are the fastest?
Well, it turns out that bothjson-schema and schema lack supprt for serveral properties mentioned in the spec such as divisibleBy, uniqueItems and format.
For my project I'm not currently using any unsupported properties, so I'll be choosing json-schema for it's speed.
In the future if I need to use an unsupported property, I can always just add it to json-schema myself and send a pull request :)
Modules not used
These modules were NOT tested due to each having a custom JSON schema format:
- https://github.com/observing/gatekeeper
- https://github.com/zzdhidden/EVE
- https://doffm.github.com/Onvalid/onvalid.html
- https://github.com/Baggz/Amanda
Lastly this module was also not tested because it lacks support for the 'required' attribute:
Source Code
Here are the JSON data objects and schema used for the benchmarks: