Skip to content

Use unique keys for caching results - #22

Closed
jviide wants to merge 4 commits into
developit:masterfrom
jviide:patch-02
Closed

jviide wants to merge 4 commits into
developit:masterfrom
jviide:patch-02

Conversation

@jviide

@jviide jviide commented Nov 23, 2018 •

Copy link
Copy Markdown
Collaborator

Currently the results for parsed templates get cached, the cache key being the statics joined with ,. It's possible to accidentally have two different tagged templates that have the same cache key. For example <div>Hello, World!</div> and <div>Hello${comma} World!</div> would both have <div>Hello, World!</div> as their cache key.

This pull request attemps to fix that and add a relevant test. Cache keys are now created by prefixing underscores and commas in statics by with an additional underscore _, and then joining these modified statics. This should (?) ensure that cache keys are unique. The cache object is also now created via Object.create(null) to ensure that it doesn't have a prototype and therefore keys like hasOwnProperty defined.

Unfortunately this affects the performance. Max. impact seems to be 5% slowdown in my development environment. The bundle size is also larger: Merged to the current master htm.mjs.gz takes 715 B, up from 688 B. When merged to #21 htm.mjs.gz takes 696 B.

@developit

Copy link
Copy Markdown
Owner

I think we can omit the null prototype step for this library - it's extremely unlikely to result in a collision if any kind.

For keys, why not use .join() with a long key?

@jviide

jviide commented Nov 23, 2018 •

Copy link
Copy Markdown
Collaborator Author

Yeah, the collisions are very unlikely. Reverted that change.

Using .join() with a long key is a good solution, though there still is a theoretical possibility of creating two equal keys for two different arrays of statics. May I still suggest yet another way? Creating the cache key with the code

let key = '', i = 0;
while (i < statics.length) {
  key += statics[i].length + '$' + statics[i++];
}

should give a unique cache key for every distinct array of statics, so that'd be one less thing to worry about. The solution seems to compress pretty well, merged to the current master htm.mjs.gz takes 710 B, up from 688 B. When merged to #21 htm.mjs.gz still takes 696 B. For some reason this change also seems to yield a modest performance increase in my development environment. Running yarn test three times for the current master version gives the following numbers:

Creation: 3535/s, average: 0.2828325339366516ms
Creation: 3575/s, average: 0.2797104238568032ms
Creation: 3507/s, average: 0.2851344518888097ms

After this change the numbers become:

Creation: 3665/s, average: 0.27279707992362245ms
Creation: 3675/s, average: 0.2720559262785637ms
Creation: 3626/s, average: 0.27575120363987315ms

I've made the changes suggested above to this branch and rebased it. However, say if you want to go with the .join() solution and I'll make the change.

@jviide

jviide commented Nov 23, 2018

Copy link
Copy Markdown
Collaborator Author

Force pushed the rebased version.

@jviide

jviide commented Nov 26, 2018

Copy link
Copy Markdown
Collaborator Author

Modified the implementation a bit, dropping the gzipped size to 708 B.

@jviide

jviide commented Dec 18, 2018

Copy link
Copy Markdown
Collaborator Author

Closing this in preparation to #38.

@jviide jviide closed this Dec 18, 2018
@jviide jviide mentioned this pull request Jul 2, 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.

2 participants