Optimizing the .includes() method for array searching with objects

In my current project, I am managing a large hardcoded array of objects that act as a database. These objects contain properties like title and content. One of the challenges I am facing involves enhancing the existing search functionality to return all objects containing a specific keyword in either the title or content.

At the moment, I am using a basic filtering solution, but considering the large number of objects (in the millions), I have been tasked with finding a more efficient way to perform searches accurately. The code base is primarily in Typescript/Javascript.

A snippet of how the objects are structured:

    {
        "id": "ab6fc754-0e01-5cfb-84b9-cf37c1c0cdb5",
        "title": "Collapse/expand code affecting the page footer\n",
        "content": "I have placed collapse/expand code onto one of my pages, but the footer now has a huge white space above it in the preview. Is there a way to fix this?",
        "userEmail": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ea999f99808598845baa80.........................</a>",
        "labels": ["Collapse", "Expand"],
        "creationTime": 1517833093439
    },

The current simplistic search implementation is as follows:

    paginatedData = paginatedData.filter((ticket: Ticket) => (ticket.content.toLowerCase() + ticket.title.toLowerCase()).includes(searchTerm.toLowerCase()))

To improve efficiency, I may need to consider restructuring the database without altering its core structure. Although one idea I am exploring is integrating the objects into a database like Mongo for faster searches, I am unsure if it would provide the desired speed improvements for the type of searches required.

Answer №1

Here are a few ways to optimize your JavaScript code:

  • Optimize by calling toLowerCase on the search string just once, instead of within every callback function:
const lowerSearch = searchTerm.toLowerCase();
const filteredData = paginatedData.filter(ticket => (ticket.content.toLowerCase() + ticket.title.toLowerCase()).includes(lowerSearch));
  • Consider optimizing further by potentially calling .includes separately on content and title if it suits your data structure better:
const lowerSearch = searchTerm.toLowerCase();
const filteredData = paginatedData.filter(ticket => (
    ticket.content.toLowerCase().includes(lowerSearch) ||
    ticket.title.toLowerCase().includes(lowerSearch)
));
  • Another option is to store pre-processed lower-cased versions of strings in your database for faster searches:
const lowerSearch = searchTerm.toLowerCase();
const filteredData = paginatedData.filter(ticket => (
    ticket.contentLower.includes(lowerSearch) ||
    ticket.titleLower.includes(lowerSearch)
));

If dealing with large datasets, consider utilizing a specialized database for efficient searching rather than solely relying on JavaScript filtering.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for starting JavaScript on Materialize 1.0

I need some help with initializing a materialize component using JavaScript. My understanding of JS is lacking, but I have had success using jQuery. Specifically, I am looking to use the Collapsible component. <ul class="collapsible"> <li> ...

Guide to dynamically assigning the id attribute of an HTML element using angularjs (1.x)

Given an HTML element that is a div, what is the method to assign a value to its id attribute? The value should be a combination of a scope variable and a string. ...

Whenever I attempt to run my script, the console on replit fails to function properly

As a complete beginner to Javascript, I'm having trouble getting my script to respond when I try to execute it. It just moves on to the next line and waits for me to type 'node index.js' again. I've included 2 images in an Imgur album - ...

WordPress CSS & JS files failing to enqueue properly

My attempt to integrate my CSS and JS files with WordPress has not been successful. Although my CSS works through the Custom CSS tab, this is not an ideal solution. Below is the code from my functions.php file for your reference. Can you help me troublesho ...

The callback for fs.WriteFile is triggered before the file is written to disk

I am encountering an issue with my express request callback that involves writing a file after calling a callback function. zip.addLocalFolder(`/path/to/folder`, `./`); var data = zip.toBuffer(); fs.writeFile(`path/to/download.zip`,data,function (err) { ...

Ways to identify if a resize event was caused by the soft keyboard in a mobile browser

Many have debated the soft keyboard, but I am still searching for a suitable solution to my issue. I currently have a resize function like: $(window).resize(function() { ///do stuff }); My goal is to execute the 'stuff' in that function on ...

Utilizing EJS to display dynamic data from a JSON file in a visually appealing D

*Excited about learning express! Currently, I have two files - index.ejs and script.js. The script I've written successfully fetches JSON data from an api. const fetch = require("node-fetch"); const url = '...' fetch (url) .then(resp ...

Learn how to eliminate all text characters from a string using jQuery, leaving only the numerical values behind

My webpage features a variety of different products, each with their own values. When trying to calculate the total sum of these products and display it in the shopping cart, I encountered an error displaying NaN. How can I remove this NaN from the strin ...

The token endpoint in Nuxtjs auth module's configuration for auth strategies is not being triggered

Our system has two important endpoints, namely /auth and /token. The endpoint /auth is responsible for providing the authorization code required to call /token in order to obtain an access token. The utilization of NuxtJS has made the auth module a prefer ...

Steps to resolve the days.map error:

My map function is, days.map((val)=>val) Upon consoling the days prop, I receive the following output: [Array(7)] 0: (7) ['', '', '', 'Wednesday', '', '', 'Saturday'] length: ...

What are the essential files required to begin with d3.js?

Starting off with d3.js, I've downloaded the newest version from https://github.com/dc-js/dc.js/releases. Along with the d3.js file, there are plenty of other scripts located in the src and spec directories. Is it necessary to move all of these files ...

The AngularJS error message states that there is an issue because the $resource function is

I am currently facing an issue with my controller, specifically the error message: TypeError: $resource is not a function This error is pointing to the line var Activities = $resource('/api/activities'); app.controller('AddActivityCtrl& ...

Changes to the parent state will not be reflected in the child props

When the child component PlaylistSpotify updates the state localPlaylist of its parent, I encounter an issue where the props in PlaylistSpotify do not update with the new results. I've been struggling to figure out what I'm missing or doing wrong ...

Revise the modal window

I need help with switching between two modal windows for registration and login. How can I make the modal window change when the "Sign Up" button is clicked? Here is the link to my project: https://jsfiddle.net/Alienwave/0kqj7tr1/4/ Vue.component('s ...

Performing a mass update in MongoDB with the help of mongoose

Is there a way to perform bulk upserts with Mongoose? Essentially, I want to have an array and insert each element if it does not exist, or update it if it does. (I am using custom _ids). When I try using .insert, MongoDB throws an error E11000 for duplic ...

Node-express can seamlessly switch between multiple databases dynamically

After extensive searching for a solution to my problem, I have come up empty-handed. If anyone has experience in similar situations, your help would be greatly appreciated. I have developed an application server in Node Express with MySQL as the database. ...

exploring the differences between beautifulsoup and re when conducting searches using regular expressions

When using urllib2.urlopen to fetch the source code of websites like this one, I decode the bytes and extract the code marked as applet using beautifulsoup. The code snippet may contain lines such as: <param name="G_00" value="espacio='E1' ti ...

Unattended HTML and head tags found unexpectedly

As a new programmer tackling my first RoR site with an integrated bootstrap theme, I've encountered some issues with the HTML code that are causing problems with image sharing on Facebook. The error message from the debugger indicates that there are m ...

Using jQuery, check if the input contains any phrases from the array that are suitable for children

I stumbled upon some code designed for a chat system. My plan is to implement it as a child-friendly global chat, so that I can avoid any blame associated with inappropriate content. The basic premise of the code involves checking user input against an arr ...

"click on the delete button and then hit the addButton

I have created a website where users can save and delete work hours. I am facing an issue where I can save the hours at the beginning, but once I delete them, I cannot save anything anymore. Additionally, when I reload the page, the deleted data reappears. ...