• author: Coding in Public

How to Use the Fuse.js Library for Client-Side Processing

In this article, we will explore the steps to implement the Fuse.js library for client-side processing of user input. Fuse.js is a powerful fuzzy search algorithm library that allows us to search and filter data based on user queries. We will be using this library to enhance our search functionality on the client-side.

Installing the Fuse.js Library

Before getting started, let's make sure we have the Fuse.js library installed in our project. Open your terminal and run the following command:


This will install the Fuse.js library and its dependencies into your project.

Configuring Weighted Search

Once the library is installed, we need to configure our search options to perform a weighted search. The weighted search allows us to assign different priorities or weights to the keys (fields) in our data. By specifying the weights, we can indicate which keys are more important or relevant in the search.

Let's take a look at an example. Suppose we have the following keys for our data: title, description, and date. We can assign weights to these keys based on their importance in our search. For instance, we can assign a higher weight to the title key and a lower weight to the description key.

To configure the weighted search in Fuse.js, we need to create a new instance of the Fuse class and pass in our options and data. Here is an example:

constsearchData=[// your data goes here];constsearchOptions={keys:[{name:'title',weight:1},{name:'description',weight:0.75},// add more keys and weights if needed],shouldSort:true,threshold:0.5,};constfuse=newFuse(searchData,searchOptions);

In the above example, we define the searchData which consists of the data we want to search. Then, we create the searchOptions object which includes the keys and their respective weights. We also set additional options such as shouldSort (to enable sorting of the search results) and threshold (which determines the matching threshold for the search).

Performing a Fuzzy Search

Once we have configured the weighted search options, we can perform a fuzzy search using the search method provided by Fuse.js. The search method takes a query as its parameter and returns an array of search results.

Here is an example of using the search method with a query:

constquery='your search query';constsearchResults=fuse.search(query);

In the above example, we pass the query to the search method, which then returns an array of search results. Each search result contains the item itself and a score indicating the match quality.

Updating Search Results in Real-Time

To update the search results in real-time as the user types, we need to handle the input event and trigger the search whenever the input value changes. However, we want to avoid creating a new instance of Fuse.js every time the search is triggered, as it can be a performance overhead.

To handle this efficiently, we can use a conditional statement to check if a Fuse instance already exists. If it does not exist, we create a new instance. Otherwise, we use the existing instance for performing the search.

Here is an example implementation:

letfuseInstance=null;// ...functionhandleSearch(query){if(!fuseInstance){fuseInstance=newFuse(searchData,searchOptions);}constsearchResults=fuseInstance.search(query);// do something with the search results}// ...<inputtype="text"onInput={event=>handleSearch(event.target.value)}/>

In the above example, we initialize fuseInstance to null. Inside the handleSearch function, we check if fuseInstance exists. If it does not exist, we create a new instance using the pre-configured searchData and searchOptions. Then, we perform the search using fuseInstance.search(query).

By implementing this approach, we ensure that a new Fuse instance is only created once and subsequently reused for performing the search.


In this article, we have discussed how to use the Fuse.js library for client-side processing of user input. We covered the steps to configure weighted search with Fuse.js, perform a fuzzy search, and update the search results efficiently in real-time. Fuse.js provides a powerful and flexible search solution that can be easily integrated into client-side applications.

Previous Post

Enhancing the Search Functionality on Our Website

Next Post

Creating a Search Output File in Lesson Five

About The auther

New Posts

Popular Post