Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

perf: optimize selector components by using Map instead of Array.find

DarkPhoenix27048c179ba6b928
diff --git a/packages/nc-gui/components/nc/List/AggregationSelector.vue b/packages/nc-gui/components/nc/List/AggregationSelector.vue
index f335476b90..116ef2bb70 100644
--- a/packages/nc-gui/components/nc/List/AggregationSelector.vue
+++ b/packages/nc-gui/components/nc/List/AggregationSelector.vue
@@ -103,16 +103,24 @@ watch([() => props.tableId, () => props.columnId], () => {
})
+const aggregationListMap = computed(() => {
+ if (!aggregationList.value || aggregationList.value.length === 0) return new Map()
+
+ return new Map(aggregationList.value.map((agg) => [agg.value, agg]))
+})
+
const selectedAggregation = computed(() => {
- if (!aggregationList.value || aggregationList.value.length === 0) return undefined
+ if (!aggregationListMap.value || aggregationListMap.value.size === 0) return undefined
- return aggregationList.value.find((agg) => agg.value === modelValue.value) || undefined
+ return aggregationListMap.value.get(modelValue.value) || undefined
})
watch(aggregationList, (newAggregationList) => {
if (newAggregationList && newAggregationList.length > 0) {
+ const newAggregationListMap = new Map(newAggregationList.map((agg) => [agg.value, agg]))
+
// Check if current value exists in the new aggregation list
- if (modelValue.value && !newAggregationList.find((agg) => agg.value === modelValue.value)) {
+ if (modelValue.value && !newAggregationListMap.has(modelValue.value)) {
// Current value is not in the list, emit null to clear it
modelValue.value = undefined
emit('update:value', undefined)
@@ -135,6 +143,7 @@ defineExpose({
selectedAggregation,
isOpenAggregationSelectDropdown,
aggregationList,
+ aggregationListMap,
})
</script>
@@ -149,7 +158,9 @@ defineExpose({
@dblclick.stop
>
<template v-if="!disableLabel" #label>
- <div>{{ t('general.aggregation') }}</div>
+ <div>
+ <slot name="label">{{ t('general.aggregation') }}</slot>
+ </div>
</template>
<NcListDropdown
v-model:is-open="isOpenAggregationSelectDropdown"
diff --git a/packages/nc-gui/components/nc/List/ColumnSelector.vue b/packages/nc-gui/components/nc/List/ColumnSelector.vue
index 310b3b3429..48b3772e23 100644
--- a/packages/nc-gui/components/nc/List/ColumnSelector.vue
+++ b/packages/nc-gui/components/nc/List/ColumnSelector.vue
@@ -73,17 +73,25 @@ const columnList = computedAsync(async () => {
})
})
+const columnListMap = computed(() => {
+ if (!columnList.value || columnList.value.length === 0) return new Map()
+
+ return new Map(columnList.value.map((column) => [column.value, column]))
+})
+
const selectedColumn = computed(() => {
- if (!columnList.value || columnList.value.length === 0) return undefined
+ if (!columnListMap.value || columnListMap.value.size === 0) return undefined
- return columnList.value.find((column) => column.value === modelValue.value) || undefined
+ return columnListMap.value.get(modelValue.value) || undefined
})
// Watch for columnList changes and set initial value
watch(columnList, (newColumnList) => {
if (newColumnList && newColumnList.length > 0) {
+ const newColumnListMap = new Map(newColumnList.map((column) => [column.value, column]))
+
// Check if current value exists in the new column list
- if (modelValue.value && !newColumnList.find((column) => column.value === modelValue.value)) {
+ if (modelValue.value && !newColumnListMap.has(modelValue.value)) {
// Current value is not in the list, emit null to clear it
modelValue.value = undefined
emit('update:value', undefined)
@@ -94,7 +102,7 @@ watch(columnList, (newColumnList) => {
if (!modelValue.value && props.autoSelect) {
const newColumnId = props.columnId || newColumnList[0]?.value
- const columnObj = newColumnList.find((column) => column.value === newColumnId)
+ const columnObj = newColumnListMap.get(newColumnId)
// Change column id only if it is default column selected initially and its not enabled
if (columnObj && columnObj.ncItemDisabled && columnObj.value === newColumnList[0]?.value) {
@@ -114,6 +122,7 @@ defineExpose({
selectedColumn,
isOpenColumnSelectDropdown,
columnList,
+ columnListMap,
})
</script>
@@ -128,7 +137,9 @@ defineExpose({
@dblclick.stop
>
<template v-if="!disableLabel" #label>
- <div>{{ t('general.column') }}</div>
+ <div>
+ <slot name="label">{{ t('general.column') }}</slot>
+ </div>
</template>
<NcListDropdown
v-model:is-open="isOpenColumnSelectDropdown"
diff --git a/packages/nc-gui/components/nc/List/TableSelector.vue b/packages/nc-gui/components/nc/List/TableSelector.vue
index 17527d13ea..6b87c238d3 100644
--- a/packages/nc-gui/components/nc/List/TableSelector.vue
+++ b/packages/nc-gui/components/nc/List/TableSelector.vue
@@ -67,16 +67,24 @@ const tableList = computedAsync(async () => {
})
})
+const tableListMap = computed(() => {
+ if (!tableList.value || tableList.value.length === 0) return new Map()
+
+ return new Map(tableList.value.map((table) => [table.value, table]))
+})
+
const selectedTable = computed(() => {
- if (!tableList.value || tableList.value.length === 0) return undefined
+ if (!tableListMap.value || tableListMap.value.size === 0) return undefined
- return tableList.value.find((table) => table.value === modelValue.value) || undefined
+ return tableListMap.value.get(modelValue.value) || undefined
})
watch(tableList, (newTableList) => {
if (newTableList && newTableList.length > 0) {
+ const newTableListMap = new Map(newTableList.map((table) => [table.value, table]))
+
// Check if current value exists in the new table list
- if (modelValue.value && !newTableList.find((table) => table.value === modelValue.value)) {
+ if (modelValue.value && !newTableListMap.has(modelValue.value)) {
// Current value is not in the list, emit null to clear it
modelValue.value = null
emit('update:value', null)
@@ -87,7 +95,7 @@ watch(tableList, (newTableList) => {
if (!modelValue.value && props.autoSelect) {
const newTableId = props.tableId || newTableList[0]?.value
- const tableObj = newTableList.find((table) => table.value === newTableId)
+ const tableObj = newTableListMap.get(newTableId)
if (tableObj && tableObj.ncItemDisabled && tableObj.value === newTableList[0]?.value) {
const selectedValue = newTableList.find((table) => !table.ncItemDisabled)?.value || newTableList[0]?.value
@@ -106,6 +114,7 @@ defineExpose({
selectedTable,
isOpenTableSelectDropdown,
tableList,
+ tableListMap,
})
</script>
@@ -120,7 +129,9 @@ defineExpose({
@dblclick.stop
>
<template v-if="!disableLabel" #label>
- <div>{{ t('general.table') }}</div>
+ <div>
+ <slot name="label">{{ t('general.table') }}</slot>
+ </div>
</template>
<NcListDropdown
v-model:is-open="isOpenTableSelectDropdown"
diff --git a/packages/nc-gui/components/nc/List/ViewSelector.vue b/packages/nc-gui/components/nc/List/ViewSelector.vue
index 133645057a..ad52198593 100644
--- a/packages/nc-gui/components/nc/List/ViewSelector.vue
+++ b/packages/nc-gui/components/nc/List/ViewSelector.vue
@@ -79,16 +79,24 @@ const viewList = computedAsync(async () => {
})
})
+const viewListMap = computed(() => {
+ if (!viewList.value || viewList.value.length === 0) return new Map()
+
+ return new Map(viewList.value.map((view) => [view.value, view]))
+})
+
const selectedView = computed(() => {
- if (!viewList.value || viewList.value.length === 0) return undefined
+ if (!viewListMap.value || viewListMap.value.size === 0) return undefined
- return viewList.value.find((view) => view.value === modelValue.value) || viewList.value[0]
+ return viewListMap.value.get(modelValue.value) || viewList.value?.[0]
})
watch(viewList, (newViewList) => {
if (newViewList && newViewList.length > 0) {
+ const newViewListMap = new Map(newViewList.map((view) => [view.value, view]))
+
// Check if current value exists in the new view list
- if (modelValue.value && !newViewList.find((view) => view.value === modelValue.value)) {
+ if (modelValue.value && !newViewListMap.has(modelValue.value)) {
// Current value is not in the list, emit null to clear it
modelValue.value = undefined
emit('update:value', undefined)
@@ -99,7 +107,7 @@ watch(viewList, (newViewList) => {
if (!modelValue.value && props.autoSelect) {
const newViewId = props.viewId || newViewList[0]?.value
- const viewObj = newViewList.find((view) => view.value === newViewId)
+ const viewObj = newViewListMap.get(newViewId)
// Change view id only if it is default view selected initially and its not enabled
if (viewObj && viewObj.ncItemDisabled && viewObj.value === newViewList[0]?.value) {
@@ -119,6 +127,7 @@ defineExpose({
selectedView,
isOpenViewSelectDropdown,
viewList,
+ viewListMap,
})
</script>
@@ -133,7 +142,9 @@ defineExpose({
@dblclick.stop
>
<template v-if="!disableLabel" #label>
- <div>{{ t('general.view') }}</div>
+ <div>
+ <slot name="label">{{ t('general.view') }}</slot>
+ </div>
</template>
<NcListDropdown
v-model:is-open="isOpenViewSelectDropdown"