Created
January 13, 2012 19:39
-
-
Save Sembiance/1608305 to your computer and use it in GitHub Desktop.
JSON objects, schema and benchmark code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
fullName : "John Doe", | |
age : 47, | |
state : "Massachusetts", | |
city : "Boston", | |
zip : 02201, | |
married : false, | |
dozen : 12, | |
dozenOrBakersDozen : 13, | |
favoriteEvenNumber : 14, | |
topThreeFavoriteColors : [ "red", "magenta", "cyan" ], | |
favoriteSingleDigitWholeNumbers : [ 7 ], | |
favoriteFiveLetterWord : "coder", | |
emailAddresses : | |
[ | |
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@letters-in-local.org", | |
"01234567890@numbers-in-local.net", | |
"&'*+-./=?^_{}~@other-valid-characters-in-local.net", | |
"mixed-1234-in-{+^}-local@sld.net", | |
"a@single-character-in-local.org", | |
"\"quoted\"@sld.com", | |
"\"\\e\\s\\c\\a\\p\\e\\d\"@sld.com", | |
"\"quoted-at-sign@sld.org\"@sld.com", | |
"\"escaped\\\"quote\"@sld.com", | |
"\"back\\slash\"@sld.com", | |
"one-character-third-level@a.example.com", | |
"single-character-in-sld@x.org", | |
"local@dash-in-sld.com", | |
"letters-in-sld@123.com", | |
"one-letter-sld@x.org", | |
"uncommon-tld@sld.museum", | |
"uncommon-tld@sld.travel", | |
"uncommon-tld@sld.mobi", | |
"country-code-tld@sld.uk", | |
"country-code-tld@sld.rw", | |
"local@sld.newTLD", | |
"the-total-length@of-an-entire-address.cannot-be-longer-than-two-hundred-and-fifty-four-characters.and-this-address-is-254-characters-exactly.so-it-should-be-valid.and-im-going-to-add-some-more-words-here.to-increase-the-lenght-blah-blah-blah-blah-bla.org", | |
"the-character-limit@for-each-part.of-the-domain.is-sixty-three-characters.this-is-exactly-sixty-three-characters-so-it-is-valid-blah-blah.com", | |
"local@sub.domains.com" | |
], | |
ipAddresses : [ "127.0.0.1", "24.48.64.2", "192.168.1.1", "209.68.44.3", "2.2.2.2" ] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
name : "test", | |
type : "object", | |
additionalProperties : false, | |
properties : | |
{ | |
fullName : { type : "string" }, | |
age : { type : "integer" }, | |
optionalItem : { type : "string", optional : true }, | |
state : { type : "string", optional : true }, | |
city : { type : "string", optional : true }, | |
zip : { type : "integer", format : "postal-code" }, | |
married : { type : "boolean" }, | |
dozen : { type : "integer", minimum : 12, maximum : 12 }, | |
dozenOrBakersDozen : { type : "integer", minimum : 12, maximum : 13 }, | |
favoriteEvenNumber : { type : "integer", divisibleBy : 2 }, | |
topThreeFavoriteColors : { type : "array", minItems : 3, maxItems : 3, uniqueItems : true, items : { type : "string", format : "color" }}, | |
favoriteSingleDigitWholeNumbers : { type : "array", minItems : 1, maxItems : 10, uniqueItems : true, items : { type : "integer", minimum : 0, maximum : 9 }}, | |
favoriteFiveLetterWord : { type : "string", minLength : 5, maxLength : 5 }, | |
emailAddresses : { type : "array", minItems : 1, uniqueItems : true, items : { type : "string", format : "email" }}, | |
ipAddresses : { type : "array", uniqueItems : true, items : { type : "string", format : "ip-address" }}, | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
name : "test", | |
type : "object", | |
additionalProperties : false, | |
properties : | |
{ | |
fullName : { type : "string", required : true }, | |
age : { type : "integer", required : true }, | |
optionalItem : { type : "string" }, | |
state : { type : "string" }, | |
city : { type : "string" }, | |
zip : { type : "integer", required : true, format : "postal-code" }, | |
married : { type : "boolean", required : true }, | |
dozen : { type : "integer", required : true, minimum : 12, maximum : 12 }, | |
dozenOrBakersDozen : { type : "integer", required : true, minimum : 12, maximum : 13 }, | |
favoriteEvenNumber : { type : "integer", required : true, divisibleBy : 2 }, | |
topThreeFavoriteColors : { type : "array", required : true, minItems : 3, maxItems : 3, uniqueItems : true, items : { type : "string", format : "color" }}, | |
favoriteSingleDigitWholeNumbers : { type : "array", required : true, minItems : 1, maxItems : 10, uniqueItems : true, items : { type : "integer", minimum : 0, maximum : 9 }}, | |
favoriteFiveLetterWord : { type : "string", required : true, minLength : 5, maxLength : 5 }, | |
emailAddresses : { type : "array", required : true, minItems : 1, uniqueItems : true, items : { type : "string", format : "email" }}, | |
ipAddresses : { type : "array", required : true, uniqueItems : true, items : { type : "string", format : "ip-address" }}, | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
fullName : "John Doe", | |
age : 47, | |
state : "Massachusetts", | |
city : "Boston", | |
zip : 02201, | |
married : false, | |
dozen : 12, | |
dozenOrBakersDozen : 13 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
name : "test", | |
type : "object", | |
additionalProperties : false, | |
properties : | |
{ | |
fullName : { type : "string" }, | |
age : { type : "integer" }, | |
optionalItem : { type : "string", optional : true }, | |
state : { type : "string", optional : true }, | |
city : { type : "string", optional : true }, | |
zip : { type : "integer", format : "postal-code" }, | |
married : { type : "boolean" }, | |
dozen : { type : "integer", minimum : 12, maximum : 12 }, | |
dozenOrBakersDozen : { type : "integer", minimum : 12, maximum : 13 } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
name : "test", | |
type : "object", | |
additionalProperties : false, | |
properties : | |
{ | |
fullName : { type : "string", required : true }, | |
age : { type : "integer", required : true }, | |
optionalItem : { type : "string" }, | |
state : { type : "string" }, | |
city : { type : "string" }, | |
zip : { type : "integer", required : true, format : "postal-code" }, | |
married : { type : "boolean", required : true }, | |
dozen : { type : "integer", required : true, minimum : 12, maximum : 12 }, | |
dozenOrBakersDozen : { type : "integer", required : true, minimum : 12, maximum : 13 } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var test_objects = require("./test_objects"), | |
test_schema = require("./test_schema"), | |
jsv2 = require("JSV").JSV.createEnvironment("json-schema-draft-02"), | |
jsv3 = require("JSV").JSV.createEnvironment("json-schema-draft-03"), | |
schema = require("schema")(), | |
jsonschemavalidate = require("json-schema"), | |
assert = require("assert"); | |
var basicSchema2 = test_schema.basic2; | |
var basicSchema3 = test_schema.basic3; | |
var basicObject = test_objects.basic; | |
var schemaBasicSchema2 = schema.Schema.create(test_schema.basic2); | |
var jsv2BasicSchema = jsv2.createSchema(basicSchema2); | |
var jsv3BasicSchema = jsv3.createSchema(basicSchema3); | |
var jsv2BasicObect = jsv2.createInstance(basicObject); | |
var jsv3BasicObect = jsv3.createInstance(basicObject); | |
assert.strictEqual(jsv2BasicSchema.validate(jsv2BasicObect).errors.length, 0); | |
assert.strictEqual(jsv3BasicSchema.validate(jsv3BasicObect).errors.length, 0); | |
assert.strictEqual(schemaBasicSchema2.validate(basicObject).isError(), false); | |
assert.strictEqual(jsonschemavalidate.validate(basicObject, basicSchema3).errors.length, 0); | |
var advancedSchema2 = test_schema.advanced2; | |
var advancedSchema3 = test_schema.advanced3; | |
var advancedObject = test_objects.advanced; | |
var schemaAdvancedSchema2 = schema.Schema.create(test_schema.advanced2); | |
var jsv2AdvancedSchema = jsv2.createSchema(advancedSchema2); | |
var jsv3AdvancedSchema = jsv3.createSchema(advancedSchema3); | |
var jsv2AdvancedObect = jsv2.createInstance(advancedObject); | |
var jsv3AdvancedObect = jsv3.createInstance(advancedObject); | |
assert.strictEqual(jsv2AdvancedSchema.validate(jsv2AdvancedObect).errors.length, 0); | |
assert.strictEqual(jsv3AdvancedSchema.validate(jsv3AdvancedObect).errors.length, 0); | |
assert.strictEqual(schemaAdvancedSchema2.validate(advancedObject).isError(), false); | |
assert.strictEqual(jsonschemavalidate.validate(advancedObject, advancedSchema3).errors.length, 0); | |
var i=0, max=10000, start, end; | |
console.log("BASIC:"); | |
// JSV v2 | |
start = Date.now(); | |
for(i=0;i<max;i++) | |
{ | |
jsv2BasicSchema.validate(jsv2BasicObect); | |
} | |
end = Date.now(); | |
console.log("JSV v2 total time (" + (end-start) + ") and per document time: " + ((end-start)/max)); | |
// JSV v3 | |
start = Date.now(); | |
for(i=0;i<max;i++) | |
{ | |
jsv3BasicSchema.validate(jsv3BasicObect); | |
} | |
end = Date.now(); | |
console.log("JSV v3 total time (" + (end-start) + ") and per document time: " + ((end-start)/max)); | |
// schema | |
start = Date.now(); | |
for(i=0;i<max;i++) | |
{ | |
schemaBasicSchema2.validate(basicObject); | |
} | |
end = Date.now(); | |
console.log("schema total time (" + (end-start) + ") and per document time: " + ((end-start)/max)); | |
// json-schema | |
start = Date.now(); | |
for(i=0;i<max;i++) | |
{ | |
jsonschemavalidate.validate(basicObject, basicSchema3); | |
} | |
end = Date.now(); | |
console.log("json-schema total time (" + (end-start) + ") and per document time: " + ((end-start)/max)); | |
console.log("\nADVANCED:"); | |
// JSV v2 | |
start = Date.now(); | |
for(i=0;i<max;i++) | |
{ | |
jsv2AdvancedSchema.validate(jsv2AdvancedObect); | |
} | |
end = Date.now(); | |
console.log("JSV v2 total time (" + (end-start) + ") and per document time: " + ((end-start)/max)); | |
// JSV v3 | |
start = Date.now(); | |
for(i=0;i<max;i++) | |
{ | |
jsv3AdvancedSchema.validate(jsv3AdvancedObect); | |
} | |
end = Date.now(); | |
console.log("JSV v3 total time (" + (end-start) + ") and per document time: " + ((end-start)/max)); | |
// schema | |
start = Date.now(); | |
for(i=0;i<max;i++) | |
{ | |
schemaAdvancedSchema2.validate(advancedObject); | |
} | |
end = Date.now(); | |
console.log("schema total time (" + (end-start) + ") and per document time: " + ((end-start)/max)); | |
// json-schema | |
start = Date.now(); | |
for(i=0;i<max;i++) | |
{ | |
jsonschemavalidate.validate(advancedObject, advancedSchema3); | |
} | |
end = Date.now(); | |
console.log("json-schema total time (" + (end-start) + ") and per document time: " + ((end-start)/max)); |
Hi, I put together similar tests for v4 examples on json-schema.org page.
You can find it here: https://github.com/zaggino/z-schema/blob/master/benchmark/benchmark.js
My results:
$ node benchmark.js
tv4#basic x 26,842 ops/sec ±0.39% (98 runs sampled)
jayschema#basic x 843 ops/sec ±1.26% (93 runs sampled)
z-schema#basic x 12,995 ops/sec ±1.55% (94 runs sampled)
Fastest is tv4#basic
jayschema#advanced x 107 ops/sec ±1.39% (80 runs sampled)
z-schema#advanced x 2,294 ops/sec ±0.74% (98 runs sampled)
Fastest is z-schema#advanced
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oops again :(
I missed that z-schema is asynchronous. Not possible to do the same kind of timing as for the other libraries. How would one benchmark an asynchronous function?