Skip to content
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

[perf] Use Set so lookup of _dataListeners can be O(1) #1175

Merged
merged 1 commit into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}
}