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

fix(icon): [icon] The icon search algorithm is optimized. #2563

Merged
merged 1 commit into from
Nov 28, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions examples/sites/demos/pc/app/icon/list-composition-api.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
<tiny-input class="search-input" v-model="searchName" clearable autofocus size="small"></tiny-input>
</div>
<div class="svgs-wrapper">
<div v-for="(nameList, groupName) in iconGroups" :key="groupName">
<div class="group-name">
<div v-for="(nameList, groupName) in iconGroupsMap" :key="groupName">
<div v-show="nameList.length" class="group-name">
{{ groupName }}
</div>
<template v-for="name in nameList">
<div
v-if="searchName === '' || name.toLowerCase().includes(searchName.toLowerCase())"
:key="name"
class="svgs-item"
@click="click(name)"
>
<template v-for="name in nameList" :key="name">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

111111111

<div class="svgs-item" @click="click(name)">
<component :is="Svgs[name] && Svgs[name]()" class="svgs-icon"></component>
<span class="svgs-text">{{ name }}</span>
</div>
Expand All @@ -25,7 +20,7 @@
</template>

<script setup>
import { ref } from 'vue'
import { ref, reactive, watch } from 'vue'
import Svgs from '@opentiny/vue-icon'
import { TinyModal, TinyInput } from '@opentiny/vue'
import { iconGroups } from './iconGroups.js'
Expand All @@ -41,6 +36,7 @@ Object.keys(iconGroups).forEach((k) => {
})

const searchName = ref('')
let iconGroupsMap = reactive(iconGroups)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical: Fix reactive state management

The current implementation has a reactivity issue. Direct reassignment to a reactive object (lines 52, 67) will break Vue's reactivity system.

Refactor the reactive state management:

-let iconGroupsMap = reactive(iconGroups)
+const iconGroupsMap = ref(iconGroups)

// In watch handler
-iconGroupsMap = iconGroups
+iconGroupsMap.value = iconGroups

-iconGroupsMap = result
+iconGroupsMap.value = result

Also applies to: 52-52, 67-67


function click(name) {
window.navigator.clipboard.writeText(name)
Expand All @@ -49,6 +45,28 @@ function click(name) {
status: 'info'
})
}

watch(searchName, (newVal) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

watch 必要性不大, 还没有模板中直接过滤显得直接。

const keyWord = (newVal || '').trim().toLowerCase()
if (keyWord === '') {
iconGroupsMap = iconGroups
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

丢失响应性了

} else {
const result = {}
for (let groupName in iconGroups) {
const nameList = iconGroups[groupName]
nameList.forEach((name) => {
if (name.toLowerCase().includes(keyWord)) {
if (!result[groupName]) {
result[groupName] = [name]
} else {
result[groupName] = [...result[groupName], name]
}
}
})
}
iconGroupsMap = result
}
})
Comment on lines +49 to +69
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Optimize search algorithm performance

The current search implementation can be optimized:

  1. Unnecessary array spread operations
  2. Multiple iterations through groups
  3. Memory allocation for each search

Here's an optimized version:

 watch(searchName, (newVal) => {
   const keyWord = (newVal || '').trim().toLowerCase()
   if (keyWord === '') {
-    iconGroupsMap = iconGroups
+    iconGroupsMap.value = iconGroups
   } else {
     const result = {}
     for (let groupName in iconGroups) {
-      const nameList = iconGroups[groupName]
-      nameList.forEach((name) => {
-        if (name.toLowerCase().includes(keyWord)) {
-          if (!result[groupName]) {
-            result[groupName] = [name]
-          } else {
-            result[groupName] = [...result[groupName], name]
-          }
-        }
-      })
+      const filteredList = iconGroups[groupName].filter(name => 
+        name.toLowerCase().includes(keyWord)
+      )
+      if (filteredList.length) {
+        result[groupName] = filteredList
+      }
     }
-    iconGroupsMap = result
+    iconGroupsMap.value = result
   }
 })

This optimization:

  • Uses filter() for cleaner array operations
  • Reduces memory allocations
  • Maintains the same functionality with better performance
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
watch(searchName, (newVal) => {
const keyWord = (newVal || '').trim().toLowerCase()
if (keyWord === '') {
iconGroupsMap = iconGroups
} else {
const result = {}
for (let groupName in iconGroups) {
const nameList = iconGroups[groupName]
nameList.forEach((name) => {
if (name.toLowerCase().includes(keyWord)) {
if (!result[groupName]) {
result[groupName] = [name]
} else {
result[groupName] = [...result[groupName], name]
}
}
})
}
iconGroupsMap = result
}
})
watch(searchName, (newVal) => {
const keyWord = (newVal || '').trim().toLowerCase()
if (keyWord === '') {
iconGroupsMap.value = iconGroups
} else {
const result = {}
for (let groupName in iconGroups) {
const filteredList = iconGroups[groupName].filter(name =>
name.toLowerCase().includes(keyWord)
)
if (filteredList.length) {
result[groupName] = filteredList
}
}
iconGroupsMap.value = result
}
})

</script>

<style scoped>
Expand Down
39 changes: 29 additions & 10 deletions examples/sites/demos/pc/app/icon/list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
<tiny-input class="search-input" v-model="searchName" clearable autofocus size="small"></tiny-input>
</div>
<div class="svgs-wrapper">
<div v-for="(nameList, groupName) in iconGroups" :key="groupName">
<div class="group-name">
<div v-for="(nameList, groupName) in iconGroupsMap" :key="groupName">
<div v-show="nameList.length" class="group-name">
{{ groupName }}
</div>
<template v-for="name in nameList">
<div
v-if="searchName === '' || name.toLowerCase().includes(searchName.toLowerCase())"
:key="name"
class="svgs-item"
@click="click(name)"
>
<template v-for="name in nameList" :key="name">
<div class="svgs-item" @click="click(name)">
<component :is="Svgs[name] && Svgs[name]()" class="svgs-icon"></component>
<span class="svgs-text">{{ name }}</span>
</div>
Expand Down Expand Up @@ -47,7 +42,8 @@ export default {
return {
iconGroups,
Svgs,
searchName: ''
searchName: '',
iconGroupsMap: iconGroups
}
},
methods: {
Expand All @@ -58,6 +54,29 @@ export default {
status: 'info'
})
}
},
watch: {
searchName(newVal) {
const keyWord = (newVal || '').trim().toLowerCase()
if (keyWord === '') {
this.iconGroupsMap = iconGroups
} else {
const result = {}
for (let groupName in iconGroups) {
const nameList = iconGroups[groupName]
nameList.forEach((name) => {
if (name.toLowerCase().includes(keyWord)) {
if (!result[groupName]) {
result[groupName] = [name]
} else {
result[groupName] = [...result[groupName], name]
}
}
})
}
this.iconGroupsMap = result
}
}
Comment on lines +58 to +79
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Optimize search performance and memory usage.

While the search implementation is functional, there are several opportunities for optimization:

  1. The current implementation creates new arrays on each search
  2. The filtering logic could be more efficient

Consider these optimizations:

 watch: {
   searchName(newVal) {
     const keyWord = (newVal || '').trim().toLowerCase()
     if (keyWord === '') {
       this.iconGroupsMap = iconGroups
     } else {
       const result = {}
-      for (let groupName in iconGroups) {
-        const nameList = iconGroups[groupName]
-        nameList.forEach((name) => {
-          if (name.toLowerCase().includes(keyWord)) {
-            if (!result[groupName]) {
-              result[groupName] = [name]
-            } else {
-              result[groupName] = [...result[groupName], name]
-            }
-          }
-        })
+      for (const [groupName, nameList] of Object.entries(iconGroups)) {
+        const filteredNames = nameList.filter(name => 
+          name.toLowerCase().includes(keyWord)
+        )
+        if (filteredNames.length) {
+          result[groupName] = filteredNames
+        }
       }
       this.iconGroupsMap = result
     }
   }
 }

This optimization:

  • Reduces memory allocations
  • Uses more efficient array operations
  • Maintains the same functionality with better performance
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
watch: {
searchName(newVal) {
const keyWord = (newVal || '').trim().toLowerCase()
if (keyWord === '') {
this.iconGroupsMap = iconGroups
} else {
const result = {}
for (let groupName in iconGroups) {
const nameList = iconGroups[groupName]
nameList.forEach((name) => {
if (name.toLowerCase().includes(keyWord)) {
if (!result[groupName]) {
result[groupName] = [name]
} else {
result[groupName] = [...result[groupName], name]
}
}
})
}
this.iconGroupsMap = result
}
}
watch: {
searchName(newVal) {
const keyWord = (newVal || '').trim().toLowerCase()
if (keyWord === '') {
this.iconGroupsMap = iconGroups
} else {
const result = {}
for (const [groupName, nameList] of Object.entries(iconGroups)) {
const filteredNames = nameList.filter(name =>
name.toLowerCase().includes(keyWord)
)
if (filteredNames.length) {
result[groupName] = filteredNames
}
}
this.iconGroupsMap = result
}
}

}
}
</script>
Expand Down
Loading