I have been encountering a slowdown while filtering through a large array of items based on user input. I decided to implement Lodash debounce to address this issue, but unfortunately, it doesn't seem to be having any effect.
Below is the HTML search input field where users can type to filter through the array:
<input v-model="search" type="text"/>
Here, I am iterating through the filteredIcons
computed property to display the items:
<section v-if="icons.length">
<button v-for="(icon, index) in filteredIcons" :key="index">
<span v-html="icon.icon"></span>
<span v-text="icon.name"></span>
</button>
</section>
And here is the section where the large icons
array is filtered through the computed property:
import { debounce } from "lodash";
export default {
data() {
return {
icons: [
{
"author":"svg",
"icon":"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M13 16v5a1 1 0 01-1 1H9l-3-6a2 2 0 01-2-2 2 2 0 01-2-2v-2c0-1.1.9-2 2-2 0-1.1.9-2 2-2h7.59l4-4H20a2 2 0 012 2v14a2 2 0 01-2 2h-2.41l-4-4H13zm0-2h1.41l4 4H20V4h-1.59l-4 4H13v6zm-2 0V8H6v2H4v2h2v2h5zm0 2H8.24l2 4H11v-4z\"/></svg>",
"name":"icon-announcement.svg"
},
{
"author":"svg",
"icon":"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M20 9v10a2 2 0 01-2 2H6a2 2 0 01-2-2V9a2 2 0 01-2-2V5c0-1.1.9-2 2-2h16a2 2 0 012 2v2a2 2 0 01-2 2zm0-2V5H4v2h16zM6 9v10h12V9H6zm4 2h4a1 1 0 010 2h-4a1 1 0 010-2z\"/></svg>",
"name":"icon-archive.svg"
}
],
search: ""
};
},
computed: {
filteredIcons: {
get() {
return this.icons;
},
set: debounce(function() {
this.icons = this.icons.filter(icon => {
icon.name.toLowerCase().includes(this.search.toLowerCase());
});
}, 500)
}
}
}
I'm puzzled by why this implementation isn't working as expected and would greatly appreciate any suggestions on the best approach for efficiently filtering through a large array using debounce. Thank you for your help!