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

Increase performance slightly #31

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open

Conversation

kurtextrem
Copy link

@kurtextrem kurtextrem commented Sep 10, 2021

Benchmark: Before

Benchmarks

Benchmarks performed on:

  • native JSON.stringify
  • fast-json-stringify
  • slow-json-stringify

small-object

native x 4,193,297 ops/sec ±0.73% (93 runs sampled)
fast-json-stringify x 60,026,740 ops/sec ±0.48% (91 runs sampled)
slow-json-stringify x 61,442,849 ops/sec ±0.50% (94 runs sampled)

# slow-json-stringify is +2.36% faster

small-array

native x 370,801 ops/sec ±0.52% (94 runs sampled)
fast-json-stringify x 448,691 ops/sec ±0.43% (96 runs sampled)
slow-json-stringify x 1,331,594 ops/sec ±0.51% (96 runs sampled)

# slow-json-stringify is +196.77% faster

nested-props-short-text

native x 922,660 ops/sec ±0.46% (95 runs sampled)
fast-json-stringify x 4,190,374 ops/sec ±0.68% (94 runs sampled)
slow-json-stringify x 13,525,473 ops/sec ±0.56% (92 runs sampled)

# slow-json-stringify is +222.77% faster

much-props-short-text

native x 800,738 ops/sec ±0.38% (94 runs sampled)
fast-json-stringify x 1,047,538 ops/sec ±0.40% (95 runs sampled)
slow-json-stringify x 3,026,591 ops/sec ±0.97% (88 runs sampled)

# slow-json-stringify is +188.92% faster

much-props-big-text

native x 22,005 ops/sec ±0.68% (92 runs sampled)
fast-json-stringify x 20,376 ops/sec ±0.71% (90 runs sampled)
slow-json-stringify x 3,392,929 ops/sec ±0.49% (93 runs sampled)

# slow-json-stringify is +15318.90% faster

big-text

native x 274,816 ops/sec ±0.43% (92 runs sampled)
fast-json-stringify x 285,642 ops/sec ±0.51% (91 runs sampled)
slow-json-stringify x 62,128,675 ops/sec ±0.69% (91 runs sampled)

# slow-json-stringify is +21650.61% faster

big-array-short-text

native x 4,678 ops/sec ±0.47% (93 runs sampled)
slow-json-stringify x 11,670 ops/sec ±0.44% (93 runs sampled)

# slow-json-stringify is +149.52% faster

big-array-long-text

native x 103 ops/sec ±0.59% (76 runs sampled)
slow-json-stringify x 11,480 ops/sec ±0.62% (93 runs sampled)

# slow-json-stringify is +11044.66% faster

big-array-long-number

native x 1,301 ops/sec ±0.75% (91 runs sampled)
slow-json-stringify x 4,023 ops/sec ±1.07% (91 runs sampled)

# slow-json-stringify is +209.46% faster

undefined properties

native x 2,456,945 ops/sec ±0.60% (95 runs sampled)
fast-json-stringify x 8,646,794 ops/sec ±0.49% (93 runs sampled)
slow-json-stringify x 23,159,091 ops/sec ±0.57% (89 runs sampled)

# slow-json-stringify is +167.83% faster
After

Benchmarks

Benchmarks performed on:

  • native JSON.stringify
  • fast-json-stringify
  • slow-json-stringify

small-object

native x 4,418,329 ops/sec ±0.36% (91 runs sampled)
fast-json-stringify x 62,240,693 ops/sec ±0.41% (93 runs sampled)
slow-json-stringify x 70,296,883 ops/sec ±0.49% (94 runs sampled)

# slow-json-stringify is +12.94% faster

small-array

native x 382,636 ops/sec ±0.39% (91 runs sampled)
fast-json-stringify x 355,617 ops/sec ±0.34% (95 runs sampled)
slow-json-stringify x 1,504,099 ops/sec ±0.42% (93 runs sampled)

# slow-json-stringify is +293.09% faster

nested-props-short-text

native x 958,195 ops/sec ±0.48% (92 runs sampled)
fast-json-stringify x 4,036,352 ops/sec ±0.48% (93 runs sampled)
slow-json-stringify x 16,360,174 ops/sec ±0.78% (86 runs sampled)

# slow-json-stringify is +305.32% faster

much-props-short-text

native x 860,514 ops/sec ±0.42% (92 runs sampled)
fast-json-stringify x 1,067,713 ops/sec ±0.47% (97 runs sampled)
slow-json-stringify x 3,505,841 ops/sec ±0.67% (92 runs sampled)

# slow-json-stringify is +228.35% faster

much-props-big-text

native x 22,928 ops/sec ±0.69% (94 runs sampled)
fast-json-stringify x 21,234 ops/sec ±0.81% (92 runs sampled)
slow-json-stringify x 3,854,902 ops/sec ±0.38% (93 runs sampled)

# slow-json-stringify is +16713.08% faster

big-text

native x 284,610 ops/sec ±0.41% (92 runs sampled)
fast-json-stringify x 299,126 ops/sec ±0.38% (95 runs sampled)
slow-json-stringify x 74,967,337 ops/sec ±1.44% (89 runs sampled)

# slow-json-stringify is +24962.13% faster

big-array-short-text

native x 4,885 ops/sec ±0.57% (91 runs sampled)
slow-json-stringify x 12,722 ops/sec ±0.63% (91 runs sampled)

# slow-json-stringify is +160.46% faster

big-array-long-text

native x 108 ops/sec ±0.55% (79 runs sampled)
slow-json-stringify x 12,935 ops/sec ±0.26% (91 runs sampled)

# slow-json-stringify is +11876.85% faster

big-array-long-number

native x 1,377 ops/sec ±0.35% (94 runs sampled)
slow-json-stringify x 4,512 ops/sec ±0.91% (89 runs sampled)

# slow-json-stringify is +227.91% faster

undefined properties

native x 2,656,347 ops/sec ±0.50% (92 runs sampled)
fast-json-stringify x 8,344,745 ops/sec ±0.30% (96 runs sampled)
slow-json-stringify x 26,317,667 ops/sec ±0.46% (92 runs sampled)

# slow-json-stringify is +215.38% faster

Run on a 2019 MB Pro i7 2,6 Ghz.

This is achieved by:

  • rewriting some native "map" functions to for loops
  • using ?. syntax instead of ||{} in find (but I also optimized the old _find - you can take a look)
  • hoisting regexes & functions

(cc developit/microbundle#892 for more mangling 'performance', since the hoisting lead to less byte savings in the bundle)

mangle.json Outdated
"properties": {
"regex": "/^_/"
},
"toplevel": true

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toplevel is true by default.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

mangle.json Outdated
},
"compress": {
"reduce_funcs": false,
"passes": 2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No real reason to limit passes - Terser will stop processing once two subsequent passes result in no change.

Suggested change
"passes": 2
"passes": 10

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh this is very interesting to know, thank you again!

package.json Outdated Show resolved Hide resolved
@cjk
Copy link

cjk commented Jan 5, 2022

This looks promising, why is it stalled?

@cjk
Copy link

cjk commented Jan 7, 2022

Thank you! 🎉

@kurtextrem
Copy link
Author

Thank you! 🎉

There is not much more to optimize on the hot path as far as I can see.

@MarcoLeko
Copy link

Is there anything we can do about to make these changes mergeable? Seems like this PR got stale for a few months already

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

Successfully merging this pull request may close these issues.

None yet

4 participants