Skip to content

Commit

Permalink
Refactor cache into Map (#1364)
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Sep 26, 2020
1 parent f8fed49 commit 8232941
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
8 changes: 4 additions & 4 deletions lib/autoprefixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function isPlainObject (obj) {
return Object.prototype.toString.apply(obj) === '[object Object]'
}

let cache = {}
let cache = new Map()

function timeCapsule (result, prefixes) {
if (prefixes.browsers.selected.length === 0) {
Expand Down Expand Up @@ -103,11 +103,11 @@ module.exports = (...reqs) => {
let browsers = new Browsers(d.browsers, reqs, opts, brwlstOpts)
let key = browsers.selected.join(', ') + JSON.stringify(options)

if (!cache[key]) {
cache[key] = new Prefixes(d.prefixes, browsers, options)
if (!cache.has(key)) {
cache.set(key, new Prefixes(d.prefixes, browsers, options))
}

return cache[key]
return cache.get(key)
}

return {
Expand Down
13 changes: 5 additions & 8 deletions lib/prefixes.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Value.hack(require('./hacks/display-flex'))
Value.hack(require('./hacks/display-grid'))
Value.hack(require('./hacks/filter-value'))

let declsCache = {}
let declsCache = new Map()

class Prefixes {
constructor (data, browsers, options = {}) {
Expand Down Expand Up @@ -267,14 +267,11 @@ class Prefixes {
* Declaration loader with caching
*/
decl (prop) {
let decl = declsCache[prop]

if (decl) {
return decl
} else {
declsCache[prop] = Declaration.load(prop)
return declsCache[prop]
if (!declsCache.has(prop)) {
declsCache.set(prop, Declaration.load(prop))
}

return declsCache.get(prop)
}

/**
Expand Down
17 changes: 8 additions & 9 deletions lib/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let utils = require('./utils')
class Selector extends Prefixer {
constructor (name, prefixes, all) {
super(name, prefixes, all)
this.regexpCache = {}
this.regexpCache = new Map()
}

/**
Expand All @@ -33,16 +33,15 @@ class Selector extends Prefixer {
* Lazy loadRegExp for name
*/
regexp (prefix) {
if (this.regexpCache[prefix]) {
return this.regexpCache[prefix]
if (!this.regexpCache.has(prefix)) {
let name = prefix ? this.prefixed(prefix) : this.name
this.regexpCache.set(
prefix,
new RegExp(`(^|[^:"'=])${utils.escapeRegexp(name)}`, 'gi')
)
}

let name = prefix ? this.prefixed(prefix) : this.name
this.regexpCache[prefix] = new RegExp(
`(^|[^:"'=])${utils.escapeRegexp(name)}`,
'gi'
)
return this.regexpCache[prefix]
return this.regexpCache.get(prefix)
}

/**
Expand Down

0 comments on commit 8232941

Please sign in to comment.