Skip to content

Commit 0c3b431

Browse files
authored
perf: use map for sting-based stores (#383)
1 parent cc1b175 commit 0c3b431

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

lib/strategies/accept-host.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
const assert = require('node:assert')
33

44
function HostStorage () {
5-
const hosts = {}
5+
const hosts = new Map()
66
const regexHosts = []
77
return {
88
get: (host) => {
9-
const exact = hosts[host]
9+
const exact = hosts.get(host)
1010
if (exact) {
1111
return exact
1212
}
@@ -20,7 +20,7 @@ function HostStorage () {
2020
if (host instanceof RegExp) {
2121
regexHosts.push({ host, value })
2222
} else {
23-
hosts[host] = value
23+
hosts.set(host, value)
2424
}
2525
}
2626
}

lib/strategies/accept-version.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ function SemVerStore () {
77
return new SemVerStore()
88
}
99

10-
this.store = {}
11-
10+
this.store = new Map()
1211
this.maxMajor = 0
1312
this.maxMinors = {}
1413
this.maxPatches = {}
@@ -30,29 +29,29 @@ SemVerStore.prototype.set = function (version, store) {
3029

3130
if (major >= this.maxMajor) {
3231
this.maxMajor = major
33-
this.store.x = store
34-
this.store['*'] = store
35-
this.store['x.x'] = store
36-
this.store['x.x.x'] = store
32+
this.store.set('x', store)
33+
this.store.set('*', store)
34+
this.store.set('x.x', store)
35+
this.store.set('x.x.x', store)
3736
}
3837

3938
if (minor >= (this.maxMinors[major] || 0)) {
4039
this.maxMinors[major] = minor
41-
this.store[`${major}.x`] = store
42-
this.store[`${major}.x.x`] = store
40+
this.store.set(`${major}.x`, store)
41+
this.store.set(`${major}.x.x`, store)
4342
}
4443

4544
if (patch >= (this.maxPatches[`${major}.${minor}`] || 0)) {
4645
this.maxPatches[`${major}.${minor}`] = patch
47-
this.store[`${major}.${minor}.x`] = store
46+
this.store.set(`${major}.${minor}.x`, store)
4847
}
4948

50-
this.store[`${major}.${minor}.${patch}`] = store
49+
this.store.set(`${major}.${minor}.${patch}`, store)
5150
return this
5251
}
5352

5453
SemVerStore.prototype.get = function (version) {
55-
return this.store[version]
54+
return this.store.get(version)
5655
}
5756

5857
module.exports = {

lib/strategies/http-method.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
module.exports = {
44
name: '__fmw_internal_strategy_merged_tree_http_method__',
55
storage: function () {
6-
const handlers = {}
6+
const handlers = new Map()
77
return {
8-
get: (type) => { return handlers[type] || null },
9-
set: (type, store) => { handlers[type] = store }
8+
get: (type) => { return handlers.get(type) || null },
9+
set: (type, store) => { handlers.set(type, store) }
1010
}
1111
},
1212
/* c8 ignore next 1 */

0 commit comments

Comments
 (0)