Repository navigation
Conversation
|
Pushed an update that brings down the compressed sizes without affecting the benchmarks: |
|
Pushed three suggested commits that can be rolled back if needed:
These changes together make the code size smaller, but with some performance loss (mostly from multi-root support, creation benchmark still being faster than the master version): |
|
It should be mentioned that at least one @mikesherov's size optimization from #48 is also included in this PR. |
|
Ha, beat me to the punch. I was literally fiddling around with getting rid of charAt and charCodeAt myself. |
mikesherov
left a comment
There was a problem hiding this comment.
Just a couple of more potential size opts.
| // char is a quote && (quote === char || quote is empty) | ||
| quote = quote ? '' : char; | ||
| } | ||
| else if (idx === 2) { |
There was a problem hiding this comment.
You could attempt < 3 here (if perf is a non-issue in this case), same for the other else ifs.
There was a problem hiding this comment.
Huge thanks for the review! Seems that compression is a fickle beast - <s brought some file sizes up, some down. So maybe === in this case?
| current = [current]; | ||
| mode = MODE_TAGNAME; | ||
| } | ||
| else if (mode !== MODE_TEXT && (idx = '\'">=/\t\n\r '.indexOf(char)) >= 0 && char.indexOf(quote) >= 0) { |
There was a problem hiding this comment.
Can replace the >= 0 with prefix ~ if it helps.
There was a problem hiding this comment.
Also, maybe you did it for compressibility, but char === quote instead of char.indexOf(quote) >= 0, no?
There was a problem hiding this comment.
~ brought some file sizes up, some down, so I kept the >= as it's probably a bit more explicit (YMMV).
The char.indexOf part was there as a shorthand for char === quote || quote === ''. But now that you mention it, char === quote || !quote seems be just as good for compressibility (some files up, some files down) and quite a bit clearer in intent 😅
There was a problem hiding this comment.
File sizes after removing char.indexOf:
Build "htm" to dist:
583 B: htm.mjs.gz
519 B: htm.mjs.br
649 B: htm.umd.js.gz
579 B: htm.umd.js.br
There was a problem hiding this comment.
the explicit checks should also improve performance a tiny bit!
| current = [current]; | ||
| mode = MODE_TAGNAME; | ||
| } | ||
| else if (mode !== MODE_TEXT && (char === quote || !quote) && (idx = '\'">=/\t\n\r '.indexOf(char)) >= 0) { |
There was a problem hiding this comment.
This might be quite a bit more expensive than char.charCodeAt(0) + integer comparisons. Any idea if it affected the benchmarks much?
There was a problem hiding this comment.
Interestingly, in my environment, the char = statics[i][j] approach seems to be the fastest:
Build "htm" to dist:
604 B: htm.mjs.gz
537 B: htm.mjs.br
672 B: htm.umd.js.gz
591 B: htm.umd.js.br
Creation: 41,385/s, average: 24µs
Usage: 348,137/s, average: 2µs
.indexOf makes it a bit slower, but more compact:
Build "htm" to dist:
583 B: htm.mjs.gz
519 B: htm.mjs.br
649 B: htm.umd.js.gz
579 B: htm.umd.js.br
Creation: 40,209/s, average: 24µs
Usage: 342,160/s, average: 2µs
The perf of the .charCodeAt approach is pretty much the same as of .indexOf, but with increased size:
Build "htm" to dist:
617 B: htm.mjs.gz
556 B: htm.mjs.br
684 B: htm.umd.js.gz
601 B: htm.umd.js.br
Creation: 39,666/s, average: 25µs
Usage: 344,306/s, average: 2µs
| } | ||
|
|
||
| for (let j=0; j<statics[i].length; j++) { | ||
| char = statics[i][j]; |
There was a problem hiding this comment.
may be benefit to using = statics[i][j|0]:
There was a problem hiding this comment.
This doesn't seem to have a notable effect (in my environment).
|
Rolling back the multi-root support but keeping everything else in this PR gives the following numbers: |
|
After the latest code changes the code sizes are (note that this version includes the multi-root support): And the perf-test-harness benchmarks: |
Use Map for caching when available, otherwise fall back to the old caching scheme based on string keys. These changes were code-golfed to their current form together with @developit.
|
The latest version is one we code-golfed together with @developit. It uses Map for caching when available, and otherwise falls back to current the string key based caching scheme. This boosts performance quite a bit on platforms where a native Map implementation is available, with no performance loss on platforms that don't. The current benchmark numbers on a platform that supports Map natively are: The corresponding numbers on the current master branch are: The file sizes got bigger, but mostly stayed under 600 bytes: |
As pointed out in #51, the current version of HTM uses code evaluation. This prohibits setting a strict Content-Security Policy.
This pull request implements HTM without code generation. Instead the parsing phase generates a list of "instructions", which the result can be evaluated from. For each instruction we append to the list FIELD, VALUE and OP, and for one special instruction (PROPS_SET) we append an additional KEY after the OP. Possible OPs are:
FIELD ? fields[FIELD] : VALUEKEYtoFIELD ? fields[FIELD] : VALUEFIELD ? fields[FIELD] : VALUEto propsFIELD ? fields[FIELD] : VALUEand append to childrenFIELD ? fields[FIELD] : VALUEto childrenFor some of these OPs for example VALUE is not relevant, but they're there anyway (set to
0) to keep the evaluation code compact.To compress the parsing logic
MODE_SKIPis now replaced byMODE_SLASH, signifying "we're still parsing a tag, but have encountered a slash, so ignore everything until a TAG_END".The .gz/.br code size is smaller compared to the current master branch version. The "creation" benchmark numbers are higher, but the "usage" benchmark is lower. Note that the benchmarks were run without Jest.
Before change
After change
The Babel plugin needed to be modified, as the props assignment pattern changed.