Skip to content

Size optimizations for src/index.mjs - #48

Closed
jviide wants to merge 14 commits into
developit:masterfrom
jviide:size-opt
Closed

jviide wants to merge 14 commits into
developit:masterfrom
jviide:size-opt

Conversation

@jviide

@jviide jviide commented Dec 21, 2018

Copy link
Copy Markdown
Collaborator

This pull request optimizes the size of the minified & compressed library. Mostly it's using the trick is collapsing a state flag and a appendable string together, like propsClose previously.

  • Remove inTag by using the mode variable for it instead (we're not in a tag if mode === MODE_TEXT).
  • Spread code repeated Object.assign.
  • Keep track if child and tag states with childClose and tagClose.
  • Replace propHasValue with fallbackPropValue that can be stringified instead when buffer is empty.

These changes together have the following effect on the library size:

Before change:

Build "htm" to dist:
        649 B: htm.mjs.gz
        596 B: htm.mjs.br
        714 B: htm.umd.js.gz
        645 B: htm.umd.js.br

After change:

Build "htm" to dist:
        605 B: htm.mjs.gz
        546 B: htm.mjs.br
        670 B: htm.umd.js.gz
        599 B: htm.umd.js.br

@jviide

jviide commented Dec 21, 2018

Copy link
Copy Markdown
Collaborator Author

Technically this allows replacing the "under 700 bytes" claim with "under 600 bytes" 😉

Comment thread src/index.mjs Outdated
}
props += propsClose + ',' + field;
propsClose = '';
else if (mode === MODE_WHITESPACE && buffer === '...') {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Just sanity-checking: if we're not in MODE_TEXT but are in MODE_WHITESPACE, we're guaranteed to be in a tag. Sound right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, whenever we're not in MODE_TEXT then we're in a tag.

Comment thread src/index.mjs Resolved
Comment thread src/index.mjs Resolved
Thanks to @mikesherov for spotting this!
Comment thread src/index.mjs Resolved
Thanks to @mikesherov for pointing this out!
@jviide

jviide commented Dec 22, 2018

Copy link
Copy Markdown
Collaborator Author

The situation after the changes suggested by @mikesherov 🙌

Before this PR

Build "htm" to dist:
        649 B: htm.mjs.gz
        596 B: htm.mjs.br
        714 B: htm.umd.js.gz
        645 B: htm.umd.js.br

After this PR

Build "htm" to dist:
        600 B: htm.mjs.gz
        537 B: htm.mjs.br
        667 B: htm.umd.js.gz
        592 B: htm.umd.js.br

Comment thread src/index.mjs Resolved
@mikesherov

Copy link
Copy Markdown

Caught one more thing that isn’t in the diff. Can replace the two instances of quote === 0 with !quote

@jviide

jviide commented Dec 23, 2018

Copy link
Copy Markdown
Collaborator Author

Yup, I tried that as well as replacing mode === MODE_TEXT with just !mode as I chose MODE_TEXT = 0 just for that. However, the compressed size actually goes up in some cases and down in some. Here are the numbers after replacing quote === 0 with !quote:

Build "htm" to dist:
        600 B: htm.mjs.gz
        543 B: htm.mjs.br
        666 B: htm.umd.js.gz
        593 B: htm.umd.js.br

@developit

Copy link
Copy Markdown
Owner

I've generally had good luck sticking with stricter equality for performance, and then just using it really consistently to ensure gzip compresses things effectively.

@jviide jviide mentioned this pull request Dec 27, 2018
@jviide

jviide commented Dec 27, 2018

Copy link
Copy Markdown
Collaborator Author

Backported size optimizations from PR #53, the numbers are now the following (benchmarks executed without Jest):

Before change

Build "htm" to dist:
        600 B: htm.mjs.gz
        537 B: htm.mjs.br
        667 B: htm.umd.js.gz
        592 B: htm.umd.js.br
Creation: 11,887/s, average: 84µs
Usage: 551,669/s, average: 1µs

After change

Build "htm" to dist:
        563 B: htm.mjs.gz
        516 B: htm.mjs.br
        629 B: htm.umd.js.gz
        561 B: htm.umd.js.br
Creation: 12,066/s, average: 82µs
Usage: 574,089/s, average: 1µs

@jviide

jviide commented Dec 27, 2018

Copy link
Copy Markdown
Collaborator Author

Added multi-root support, benchmarks are not affected and the file sizes are:

Build "htm" to dist:
        589 B: htm.mjs.gz
        536 B: htm.mjs.br
        657 B: htm.umd.js.gz
        585 B: htm.umd.js.br

@jviide

jviide commented Jan 16, 2019

Copy link
Copy Markdown
Collaborator Author

After the latest changes the code sizes are:

Build "htm" to dist:
        561 B: htm.mjs.gz
        518 B: htm.mjs.br
        630 B: htm.umd.js.gz
        566 B: htm.umd.js.br

Benchmarks seem to be unaffected.

@jviide

jviide commented Feb 20, 2019

Copy link
Copy Markdown
Collaborator Author

Closing this in favor of #62. Your size optimizations live on there, @mikesherov! 🙂

@jviide jviide closed this Feb 20, 2019
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.

3 participants