Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google clousure compiler #748

Open
2 tasks done
cesco69 opened this issue Nov 27, 2024 · 4 comments
Open
2 tasks done

Google clousure compiler #748

cesco69 opened this issue Nov 27, 2024 · 4 comments

Comments

@cesco69
Copy link
Contributor

cesco69 commented Nov 27, 2024

Prerequisites

  • I have written a descriptive issue title
  • 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 #633

@cesco69 cesco69 changed the title Google clousure compiler when standalone Google clousure compiler Nov 27, 2024
@jsumners
Copy link
Member

This project is plain JavaScript. There isn't any need for compiling anything.

@cesco69
Copy link
Contributor Author

cesco69 commented Nov 27, 2024

This project is plain JavaScript. There isn't any need for compiling anything.

google-closure-compiler is a plain javascript minifier/optimizer

@jsumners
Copy link
Member

I suggest providing some evidence that this would be an improvement/necessary. In my opinion, it will just complicate maintenance and debugging.

@nigrosimone
Copy link
Contributor

nigrosimone commented Nov 27, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants