Skip to content

Commit a7873f0

Browse files
committed
Basic implementation supporting string and integers.
1 parent b334a85 commit a7873f0

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed

bench.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict'
2+
3+
const benchmark = require('benchmark')
4+
const suite = new benchmark.Suite()
5+
6+
const schema = {
7+
"title": "Example Schema",
8+
"type": "object",
9+
"properties": {
10+
"firstName": {
11+
"type": "string"
12+
},
13+
"lastName": {
14+
"type": "string"
15+
},
16+
"age": {
17+
"description": "Age in years",
18+
"type": "integer",
19+
"minimum": 0
20+
}
21+
},
22+
"required": ["firstName", "lastName"]
23+
}
24+
25+
const obj = {
26+
firstName: 'Matteo',
27+
lastName: 'Collina',
28+
age: 32
29+
}
30+
31+
const stringify = require('.')(schema)
32+
33+
suite.add('JSON.stringify', function () {
34+
JSON.stringify(obj)
35+
})
36+
37+
suite.add('fast-json-stringify', function () {
38+
stringify(obj)
39+
})
40+
41+
suite.on('complete', print)
42+
43+
suite.run()
44+
45+
function print () {
46+
for (var i = 0; i < this.length; i++) {
47+
console.log(this[i].toString())
48+
}
49+
50+
console.log('Fastest is', this.filter('fastest').map('name')[0])
51+
}

index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict'
2+
3+
function build (schema) {
4+
5+
let code = `
6+
'use strict'
7+
let json = '{'
8+
`
9+
10+
Object.keys(schema.properties).forEach((key, i, a) => {
11+
12+
const type = schema.properties[key].type
13+
14+
code += `
15+
json += '"${key}":'
16+
`
17+
18+
switch (type) {
19+
case 'string':
20+
code += `
21+
json += '"' + obj.${key} + '"'
22+
`
23+
break;
24+
case 'integer':
25+
code += `
26+
json += '' + obj.${key}
27+
`
28+
break;
29+
default:
30+
throw new Error(`${type} unsupported`)
31+
}
32+
33+
34+
if (i < a.length - 1) {
35+
code += 'json += \',\''
36+
}
37+
})
38+
39+
code += `
40+
json += '}'
41+
return json
42+
`
43+
44+
return new Function('obj', code)
45+
}
46+
47+
module.exports = build

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "fast-json-stringify",
3+
"version": "0.0.1",
4+
"description": "Stringify your JSON at max speed",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "standard && tap test.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/mcollina/fast-json-stringify.git"
12+
},
13+
"keywords": [
14+
"json",
15+
"stringify",
16+
"schema",
17+
"fast"
18+
],
19+
"author": "Matteo Collina <[email protected]>",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/mcollina/fast-json-stringify/issues"
23+
},
24+
"homepage": "https://github.com/mcollina/fast-json-stringify#readme",
25+
"devDependencies": {
26+
"benchmark": "^2.1.1",
27+
"is-my-json-valid": "^2.13.1",
28+
"standard": "^7.1.2",
29+
"tap": "^6.2.0"
30+
}
31+
}

test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const build = require('.')
5+
6+
const example = {
7+
"title": "Example Schema",
8+
"type": "object",
9+
"properties": {
10+
"firstName": {
11+
"type": "string"
12+
},
13+
"lastName": {
14+
"type": "string"
15+
},
16+
"age": {
17+
"description": "Age in years",
18+
"type": "integer",
19+
"minimum": 0
20+
}
21+
},
22+
"required": ["firstName", "lastName"]
23+
}
24+
25+
test('render a basic json', (t) => {
26+
t.plan(2)
27+
28+
const stringify = build(example)
29+
const obj = {
30+
firstName: 'Matteo',
31+
lastName: 'Collina',
32+
age: 32
33+
}
34+
35+
const output = stringify(obj)
36+
37+
t.deepEqual(JSON.parse(output), obj)
38+
t.equal(output, JSON.stringify(obj))
39+
})

0 commit comments

Comments
 (0)