From d38c236e68edd4092cf6ac123f9917070ec7104a Mon Sep 17 00:00:00 2001 From: TATSUNO Yasuhiro Date: Fri, 9 Apr 2021 20:54:45 +0900 Subject: [PATCH] perf: use set so lookup can be O(1) instead of O(n) --- src/index.js | 13 ++++++------- src/util.js | 11 ++++------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/index.js b/src/index.js index d0d08610e..a0a7d5496 100644 --- a/src/index.js +++ b/src/index.js @@ -56,7 +56,7 @@ export default class VueI18n { _dateTimeFormatters: Object _numberFormatters: Object _path: I18nPath - _dataListeners: Array + _dataListeners: Set _componentInstanceCreatedListener: ?ComponentInstanceCreatedListener _preserveDirectiveContent: boolean _warnHtmlInMessage: WarnHtmlInMessageLevel @@ -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 @@ -240,7 +240,7 @@ export default class VueI18n { } subscribeDataChanging (vm: any): void { - this._dataListeners.push(vm) + this._dataListeners.add(vm) } unsubscribeDataChanging (vm: any): void { @@ -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 }) } diff --git a/src/util.js b/src/util.js index f96fcaffb..dd600806e 100644 --- a/src/util.js +++ b/src/util.js @@ -102,12 +102,9 @@ export function looseClone (obj: Object): Object { return JSON.parse(JSON.stringify(obj)) } -export function remove (arr: Array, item: any): Array | void { - if (arr.length) { - const index = arr.indexOf(item) - if (index > -1) { - return arr.splice(index, 1) - } +export function remove (arr: Set, item: any): Set | void { + if (arr.delete(item)) { + return arr } } @@ -203,4 +200,4 @@ export function escapeParams(params: any): any { }) } return params -} \ No newline at end of file +}