Skip to content

Commit

Permalink
Replace WeakMap with an LRU Cache
Browse files Browse the repository at this point in the history
This works around a shortcoming in template tags noted in
tc39/ecma262#840

The static tag cache now has bounded memory usage
assuming constant cache entry size.
  • Loading branch information
mikesamuel committed Feb 16, 2018
1 parent af1e77b commit 059769c
Show file tree
Hide file tree
Showing 6 changed files with 1,135 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
service_name: travis-pro
repo_token: EoBseKYwETwcI9SULqJCbpZ2wV0hJi87u
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
language: node_js
node_js:
- "6"
- "7"
- "8"
- "9"
- "stable"

# Use faster Docker architecture on Travis.
sudo: false

script: npm test
script: npm run-script lint && npm test
after_success: npm run coveralls
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Provides utilities to simplify creating template tag handlers.
*/

const { LruCache } = require('an-lru-cache')
const { every } = Array.prototype

function isString (val) {
Expand Down Expand Up @@ -123,7 +124,7 @@ let configurableTemplateTag // eslint-disable-line
* computeResultHelper.
*/
function memoizedTagFunction (computeStaticHelper, computeResultHelper) {
const memoTable = new WeakMap()
const memoTable = new LruCache()

/**
* @param {!Array.<string>} staticStrings
Expand Down Expand Up @@ -351,9 +352,14 @@ function cook (trv) {
*/
class TypedString {
constructor (content) {
Object.defineProperty(
this, 'content',
{ configurable: false, writable: false, value: String(content) })
Object.defineProperties(
this,
{
'content':
{ configurable: false, writable: false, value: String(content) },
'constructor':
{ configurable: false, writable: false, value: this.constructor }
})
}
toString () {
return this.content
Expand Down
Loading

0 comments on commit 059769c

Please sign in to comment.