fast-json-stringify
fast-json-stringify copied to clipboard
Google clousure compiler
Prerequisites
- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the feature has not already been requested
🚀 Feature Proposal
The idea is compile the stringify method with https://www.npmjs.com/package/google-closure-compiler in ADVANCED mode (https://developers.google.com/closure/compiler/docs/api-tutorial3)
The main goal of google-closure-compiler is reduce the size of the script, but:
Some of the compiler's optimizations even decrease runtime. [...] Optimizations that reduce redundancies speed up the run time of code as well.
is something like this https://github.com/fastify/fast-json-stringify/pull/633
This project is plain JavaScript. There isn't any need for compiling anything.
This project is plain JavaScript. There isn't any need for compiling anything.
google-closure-compiler is a plain javascript minifier/optimizer
I suggest providing some evidence that this would be an improvement/necessary. In my opinion, it will just complicate maintenance and debugging.
this is just a try with terser (another javascript compressor/optimizer):
'use strict'
const fastJson = require('fast-json-stringify')
const { minify } = require("terser"); // nmp install terser
const stringify = fastJson({
title: 'Example Schema',
type: 'object',
properties: {
firstName: {
type: 'string'
},
lastName: {
type: 'string'
},
age: {
description: 'Age in years',
type: 'integer'
},
now: {
type: 'string'
},
birthdate: {
type: ['string'],
format: 'date-time'
},
reg: {
type: 'string'
},
obj: {
type: 'object',
properties: {
bool: {
type: 'boolean'
}
}
},
arr: {
type: 'array',
items: {
type: 'object',
properties: {
str: {
type: 'string'
}
}
}
}
},
required: ['now'],
patternProperties: {
'.*foo$': {
type: 'string'
},
test: {
type: 'number'
},
date: {
type: 'string',
format: 'date-time'
}
},
additionalProperties: {
type: 'string'
}
}, {mode: 'standalone'})
const fs = require('fs')
const path = require('path')
minify(stringify).then(result => {
fs.writeFileSync(path.join(__dirname, 'terser.js'), result.code, {encoding: 'utf-8'})
fs.writeFileSync(path.join(__dirname, 'raw.js'), stringify, {encoding: 'utf-8'})
const tfjs = require('./terser')
const rfjs = require('./raw')
const RUN = 5000;
const OBJ = {
firstName: 'Matteo',
lastName: 'Collina',
age: 32,
now: new Date(),
reg: /"([^"]|\\")*"/,
foo: 'hello',
numfoo: 42,
test: 42,
strtest: '23',
arr: [{ str: 'stark' }, { str: 'lannister' }],
obj: { bool: true },
notmatch: 'valar morghulis',
notmatchobj: { a: true },
notmatchnum: 42
};
console.time('terser')
for (let index = 0; index < RUN; index++) {
tfjs(OBJ)
}
console.timeEnd('terser')
console.time('raw')
for (let index = 0; index < RUN; index++) {
rfjs(OBJ)
}
console.timeEnd('raw')
})
result:
terser: 40.64ms
raw: 45.999ms