-
Notifications
You must be signed in to change notification settings - Fork 173
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
Use WeakMap for caching #51
Conversation
Just 'cause you mentioned the conversation, all this has been tested for over a year through hyperHTML, and these are the things to consider:
If you ditch the WeakMap module, 'cause nowadays pretty much everyone targets IE 11 as baseline, where WeakMap is already available, you most likely need to address the uniqueness of the template literal issue due bad transpilers such TypeScript (2 versions with two different bugs 'cause they can't get specs right, maybe latter works). If you think this is worth the performance improvement over standard browsers/transpiled code, you also have the module to include 😉 If you think this project should never ship in production without pre-processing, then maybe you might also ignore the WeakMap/Template improvement, and focus on avoiding CSP issues due code evaluation. I hope I've properly explained template literals gotchas, since older projects have already dealt with all of these to grant universal compatibility through any client/server engine. Regards. P.S. if size is that important, maybe this is a quick win too function html(statics) {
return '.' + statics.map(c => [c.length, c]).join('');
} |
Could the produced function be cached as a property set on the statics array? |
Nope, 'cause template literals are frozen arrays by specs |
Thanks for the good info @WebReflection. It certainly sounds like experience earned through hard work :) Your thoughts, @developit - is WeakMap caching worth the trouble for HTM? The CSP point you raised is a very valid one. PR #53 is an attempt to address that. |
I think this can be closed for the time being. |
In a Twitter conversation https://twitter.com/webreflection raised the point that template literal statics are cached. They're cached by call site, which makes them ideal for this purpose. Combining this with WeakMaps (which are also supported in IE 11) allows fast caching while avoiding cache retention of unnecessary templates.
This pull request first modifies the benchmarks to use tagged template literals whenever possible to match the most common usage pattern. After that it switches to WeakMap based caching.
The code size goes down, and the "usage" benchmark numbers improve:
Before change
After change
When I ran the benchmarks without Jest the difference in the "usage" benchmark was even more pronounced:
Before change
After change
In the thread there was a suggestion to fall back to Map if WeakMaps aren't present. I didn't implement that, because all platforms (with the exception es6-shim) support WeakMap if they support Map.
The drawback in this approach is that the library now requires WeakMap. However, it appears that platforms that don't support WeakMap natively also don't support tagged templates natively.