Skip to content

Commit 6c90070

Browse files
authored
Merge pull request #36 from SimenB/uglify
Add ability to uglify generated code
2 parents b4d8301 + aac9631 commit 6c90070

File tree

5 files changed

+98
-10
lines changed

5 files changed

+98
-10
lines changed

README.md

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ advantages reduces on large payloads.
77
Benchmarks:
88

99
```
10-
JSON.stringify array x 3,679 ops/sec ±1.01% (85 runs sampled)
11-
fast-json-stringify array x 4,618 ops/sec ±1.64% (87 runs sampled)
12-
JSON.stringify long string x 13,303 ops/sec ±1.01% (89 runs sampled)
13-
fast-json-stringify long string x 13,489 ops/sec ±0.88% (90 runs sampled)
14-
JSON.stringify short string x 4,974,749 ops/sec ±1.14% (86 runs sampled)
15-
fast-json-stringify short string x 11,030,700 ops/sec ±0.82% (89 runs sampled)
16-
JSON.stringify obj x 1,774,593 ops/sec ±1.07% (90 runs sampled)
17-
fast-json-stringify obj x 4,976,369 ops/sec ±1.00% (89 runs sampled)
10+
JSON.stringify array x 3,288 ops/sec ±5.18% (82 runs sampled)
11+
fast-json-stringify array x 1,813 ops/sec ±10.21% (71 runs sampled)
12+
fast-json-stringify-uglified array x 2,106 ops/sec ±3.23% (83 runs sampled)
13+
JSON.stringify long string x 12,933 ops/sec ±1.27% (87 runs sampled)
14+
fast-json-stringify long string x 12,221 ops/sec ±3.31% (84 runs sampled)
15+
fast-json-stringify-uglified long string x 13,256 ops/sec ±0.95% (92 runs sampled)
16+
JSON.stringify short string x 4,878,641 ops/sec ±1.14% (90 runs sampled)
17+
fast-json-stringify short string x 11,649,100 ops/sec ±0.98% (91 runs sampled)
18+
fast-json-stringify-uglified short string x 11,877,661 ops/sec ±0.91% (90 runs sampled)
19+
JSON.stringify obj x 1,705,377 ops/sec ±2.61% (87 runs sampled)
20+
fast-json-stringify obj x 2,268,915 ops/sec ±1.39% (90 runs sampled)
21+
fast-json-stringify-uglified obj x 2,243,341 ops/sec ±1.11% (89 runs sampled)
1822
```
1923

2024
#### Table of contents:
@@ -28,6 +32,7 @@ fast-json-stringify obj x 4,976,369 ops/sec ±1.00% (89 runs sampled)
2832
- <a href="#additionalProperties">`Additional Properties`</a>
2933
- <a href="#ref">`Reuse - $ref`</a>
3034
- <a href="#long">`Long integers`</a>
35+
- <a href="#uglify">`Uglify`</a>
3136
- <a href="#acknowledgements">`Acknowledgements`</a>
3237
- <a href="#license">`License`</a>
3338

@@ -319,6 +324,27 @@ const obj = {
319324
console.log(stringify(obj)) // '{"id":18446744073709551615}'
320325
```
321326

327+
<a name="uglify"></a>
328+
#### Uglify
329+
If you want to squeeze a little bit more performance out of the serialisation, at the cost of readability in the generated code, you can pass `uglify: true` as an option.
330+
Note that you have to manually install `uglify-es` in order for it to work. Only version 3 is supported.
331+
Example:
332+
```javascript
333+
334+
const stringify = fastJson({
335+
title: 'Example Schema',
336+
type: 'object',
337+
properties: {
338+
id: {
339+
type: 'integer'
340+
}
341+
}
342+
}, { uglify: true })
343+
344+
// stringify is now minified code
345+
console.log(stringify({ some: 'object' })) // '{"some":"object"}'
346+
```
347+
322348
<a name="acknowledgements"></a>
323349
## Acknowledgements
324350

bench.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ const obj = {
3636
const multiArray = []
3737

3838
const stringify = require('.')(schema)
39+
const stringifyUgly = require('.')(schema, { uglify: true })
3940
const stringifyArray = require('.')(arraySchema)
41+
const stringifyArrayUgly = require('.')(arraySchema, { uglify: true })
4042
const stringifyString = require('.')({ type: 'string' })
43+
const stringifyStringUgly = require('.')({ type: 'string', uglify: true })
4144
var str = ''
4245

4346
for (var i = 0; i < 10000; i++) {
@@ -61,6 +64,10 @@ suite.add('fast-json-stringify array', function () {
6164
stringifyArray(multiArray)
6265
})
6366

67+
suite.add('fast-json-stringify-uglified array', function () {
68+
stringifyArrayUgly(multiArray)
69+
})
70+
6471
suite.add('JSON.stringify long string', function () {
6572
JSON.stringify(str)
6673
})
@@ -69,6 +76,10 @@ suite.add('fast-json-stringify long string', function () {
6976
stringifyString(str)
7077
})
7178

79+
suite.add('fast-json-stringify-uglified long string', function () {
80+
stringifyStringUgly(str)
81+
})
82+
7283
suite.add('JSON.stringify short string', function () {
7384
JSON.stringify('hello world')
7485
})
@@ -77,6 +88,10 @@ suite.add('fast-json-stringify short string', function () {
7788
stringifyString('hello world')
7889
})
7990

91+
suite.add('fast-json-stringify-uglified short string', function () {
92+
stringifyStringUgly('hello world')
93+
})
94+
8095
suite.add('JSON.stringify obj', function () {
8196
JSON.stringify(obj)
8297
})
@@ -85,6 +100,10 @@ suite.add('fast-json-stringify obj', function () {
85100
stringify(obj)
86101
})
87102

103+
suite.add('fast-json-stringify-uglified obj', function () {
104+
stringifyUgly(obj)
105+
})
106+
88107
suite.on('cycle', cycle)
89108

90109
suite.run()

index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const fastSafeStringify = require('fast-safe-stringify')
44

5+
var uglify = null
56
let isLong
67
try {
78
isLong = require('long').isLong
@@ -74,6 +75,11 @@ function build (schema, options) {
7475
;
7576
return ${main}
7677
`
78+
79+
if (options.uglify) {
80+
code = uglifyCode(code)
81+
}
82+
7783
if (hasAdditionalPropertiesTrue(schema)) {
7884
return (new Function('fastSafeStringify', code))(fastSafeStringify)
7985
}
@@ -457,4 +463,36 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema) {
457463
}
458464
}
459465

466+
function uglifyCode (code) {
467+
if (!uglify) {
468+
loadUglify()
469+
}
470+
471+
const uglified = uglify.minify(code, { parse: { bare_returns: true } })
472+
473+
if (uglified.error) {
474+
throw uglified.error
475+
}
476+
477+
return uglified.code
478+
}
479+
480+
function loadUglify () {
481+
try {
482+
uglify = require('uglify-es')
483+
const uglifyVersion = require('uglify-es/package.json').version
484+
485+
if (uglifyVersion[0] !== '3') {
486+
throw new Error('Only version 3 of uglify-es is supported')
487+
}
488+
} catch (e) {
489+
uglify = null
490+
if (e.code === 'MODULE_NOT_FOUND') {
491+
throw new Error('In order to use uglify, you have to manually install `uglify-es`')
492+
}
493+
494+
throw e
495+
}
496+
}
497+
460498
module.exports = build

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"long": "^3.2.0",
3030
"pre-commit": "^1.1.3",
3131
"standard": "^10.0.0",
32-
"tap": "^10.3.0"
32+
"tap": "^10.3.0",
33+
"uglify-es": "^3.0.9"
3334
},
3435
"dependencies": {
3536
"fast-safe-stringify": "^1.1.11"

test/basic.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ const build = require('..')
66

77
function buildTest (schema, toStringify) {
88
test(`render a ${schema.title} as JSON`, (t) => {
9-
t.plan(3)
9+
t.plan(5)
1010

1111
const validate = validator(schema)
1212
const stringify = build(schema)
13+
const stringifyUgly = build(schema, {uglify: true})
1314
const output = stringify(toStringify)
15+
const outputUglify = stringifyUgly(toStringify)
1416

1517
t.deepEqual(JSON.parse(output), toStringify)
18+
t.deepEqual(JSON.parse(outputUglify), toStringify)
1619
t.equal(output, JSON.stringify(toStringify))
20+
t.equal(outputUglify, JSON.stringify(toStringify))
1721
t.ok(validate(JSON.parse(output)), 'valid schema')
1822
})
1923
}

0 commit comments

Comments
 (0)