Skip to content

Commit

Permalink
perf: use set so lookup can be O(1) instead of O(n) (#1175)
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Apr 9, 2021
1 parent 1d94f8a commit e3efe8b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
13 changes: 6 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class VueI18n {
_dateTimeFormatters: Object
_numberFormatters: Object
_path: I18nPath
_dataListeners: Array<any>
_dataListeners: Set<any>
_componentInstanceCreatedListener: ?ComponentInstanceCreatedListener
_preserveDirectiveContent: boolean
_warnHtmlInMessage: WarnHtmlInMessageLevel
Expand Down Expand Up @@ -105,7 +105,7 @@ export default class VueI18n {
this._dateTimeFormatters = {}
this._numberFormatters = {}
this._path = new I18nPath()
this._dataListeners = []
this._dataListeners = new Set()
this._componentInstanceCreatedListener = options.componentInstanceCreatedListener || null
this._preserveDirectiveContent = options.preserveDirectiveContent === undefined
? false
Expand Down Expand Up @@ -240,7 +240,7 @@ export default class VueI18n {
}

subscribeDataChanging (vm: any): void {
this._dataListeners.push(vm)
this._dataListeners.add(vm)
}

unsubscribeDataChanging (vm: any): void {
Expand All @@ -250,12 +250,11 @@ export default class VueI18n {
watchI18nData (): Function {
const self = this
return this._vm.$watch('$data', () => {
let i = self._dataListeners.length
while (i--) {
self._dataListeners.forEach(e => {
Vue.nextTick(() => {
self._dataListeners[i] && self._dataListeners[i].$forceUpdate()
e && e.$forceUpdate()
})
}
})
}, { deep: true })
}

Expand Down
11 changes: 4 additions & 7 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,9 @@ export function looseClone (obj: Object): Object {
return JSON.parse(JSON.stringify(obj))
}

export function remove (arr: Array<any>, item: any): Array<any> | void {
if (arr.length) {
const index = arr.indexOf(item)
if (index > -1) {
return arr.splice(index, 1)
}
export function remove (arr: Set<any>, item: any): Set<any> | void {
if (arr.delete(item)) {
return arr
}
}

Expand Down Expand Up @@ -203,4 +200,4 @@ export function escapeParams(params: any): any {
})
}
return params
}
}

0 comments on commit e3efe8b

Please sign in to comment.