Is it a bad idea to filter a large JSON object of about 6MB in AngularJS?

I am currently working on developing a barcode scanner application. The client has provided me with a 6mb CSV file containing data for various products.

One approach I am considering is parsing the CSV file into a JSON object, extracting barcodes, and then searching for the corresponding items within a 6mb in-memory JavaScript object. Would this method negatively impact performance? Is there a size limit for the file that would affect performance?

If I were to convert this project into an Ionic app, would it further decrease performance?

While I understand that creating an API server for querying would be more efficient, do you think it is absolutely necessary in this case?

EDIT: The file is downloaded once by an Angular service and stored in memory until the app is closed. Filtering would be done on this in-memory object.

Answer №1

Note: My assumption is that this code will be utilized in a single-page application scenario or any situation where it is executed repeatedly, rather than just once per page load.

Considering the average size of images nowadays, the browser should have no issue handling a 6mb file.

The real concern lies in the efficiency of looping through such a large dataset compared to optimizing it for quicker lookups. Various factors can impact performance at this stage.

JavaScript objects tend to slow down when they contain numerous keys. In my experience, performance started deteriorating around 11,000 keys or so...

Hence, I suggest breaking down the barcodes into separate categories – here's some code that groups all the 1s together, as well as the 2s and so on. This approach significantly boosts performance while handling extensive datasets.

var barcodes = {
   0: {},
   1: {},
   2: {},
   3: {},
   4: {},
   5: {},
   6: {},
   7: {},
   8: {},
   9: {}
};

function addBarcode(barcode, obj) {
   var firstNumber = barcode.charAt(0);
   barcodes[firstNumber][barcode] = obj;
}

function getBarcode(barcode) {
   var firstNumber = barcode.charAt(0);
   return barcodes[firstNumber][barcode];
}


addBarcode("1104", {
    testprop: true
});

var o = getBarcode("1104");
console.log("testProp should be true. It is ", o.testprop);

Answer №2

It is advisable to organize your data before importing it into your application. Avoid directly reading the raw CSV file; instead, create a pre-sorted .json file and possibly another .json file for pre-indexed data storage.

Consider testing the performance of a simple loop as an initial evaluation step. Sometimes, a basic loop may suffice for your requirements.

If you decide to pre-index the data, you can structure an object like the example below. This object captures every 10 thousandth UPC value for efficient retrieval:

var preIndex = [
    //{"limit" : index at which value exists, value: UPC value},
    {"limit": 0, "value": 00000001234},
    {"limit": 10000, "value": 00009871234},
    {"limit": 20000, "value": 00019938234},
    ....
]

To locate your specific UPC within this preIndexed object, your code would need to compare and select the relevant limits:

 // Identify where YourUPC falls between two values in preIndex    
// preIndex[x].value < YourUPC <= preIndex[x+1].value

// Loop through a maximum of 10 thousand items to find your target item
for(var i = preIndex[x].limit; i <= preIndex[x+1].limit; i++){
     ... // if(myData[i].UPC == YourUPC)
 }

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

Error occurs when an arrow function is called before its function definition

console.log(addB(10, 15)); function addB(a, b) { return a + b; } console.log(addC(10, 15)); const addC = (a, b) => { return a + b; }; I attempted to convert a function into an arrow function and encountered the error "Cannot access 'addC&ap ...

Photos failing to load in the image slider

Although it may seem intimidating, a large portion of the code is repetitive. Experiment by clicking on the red buttons. <body> <ul id="carousel" class="carousel"> <button id="moveSlideLeft" class="moveSlide moveSlideLeft"></button& ...

Tips for removing a row from a table

I've been working on a responsive data table where each time I click an add button at the end of a row, a new row is added with the add button turning into a delete button. Below is the code I've implemented: The Table: <table id="invoice_ta ...

sending an array from one CodeIgniter view to another view via Ajax

In the following code segments of my application, myArray is an array where each element contains a few objects that I need to use in a second view. When I use alert(myJSON);, I am able to see the array in the alert window. However, when the view loads, i ...

The icon is being displayed as text instead of the actual Fontawesome icon

Using jquery version 3.3.1 I am dynamically creating an anchor tag: let link = $("<a>"); link.attr("href", "#"); link.text("My anchor" + ' <i class="fas fa-people-carry"></i>'); However, when I try to display ...

Error: Unable to locate module: Issue discovering 'crypto' and 'fs' modules

I am currently in the process of learning React and attempting to establish a connection between my React app and my database using the following code: var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: ...

Checking the integrity of JSON information

Currently in my AJAX requests, I am manually validating each JSON entry by using the "key" in json.some_location.keys() method repeatedly. Is there a more efficient way to accomplish this, such as XML validation? Additionally, I am open to using validatio ...

What is the process for transforming <a>fai</a><b>3</b> to <fai>3</fai> with xpath within WSO2 ESB?

I have received JSON data in the following format: {"request":""AttributesCriteriaList":[ {"FieldName":"PartyBranchId","OperationType":1,"FieldValue":"-1500000000","JoinType":2}, {"FieldName":"AssetTypeId","OperationType":1,"FieldV ...

Exploring the effectiveness of React Hook Form using React Testing Library

My Component includes a form that, upon submission, utilizes Apollo's useLazyQuery to fetch data based on the form values. The form in the component is managed by React Hook Forms, with the handleSubmit controlled by RHF. <FormContainer onSubmit= ...

Can you guide me on implementing first-person controls in react-three-fiber?

I'm attempting to set up a scenario where the camera remains stationary while allowing users to rotate the view around either the y or x axis by dragging their mouse. I'm looking for something akin to the functionality seen on threejs.org/example ...

Nested Tables in JavaScript: Creating Tables within Tables

Recently, I have been analyzing student data and noticed a recurring structure. While preparing to present information on student performance within the discipline, I also became interested in showcasing a history of new students. It was suggested that hav ...

Error: Unable to access attributes of null object (specifically 'disable click')

Currently, I am integrating react-leaflet with nextjs. Within the markers, there are popups containing buttons that open another page upon click. However, I have encountered an error when navigating to the new page: Unhandled Runtime Error TypeError: Canno ...

Eliminate error class in jQuery Validate once validation is successful

I'm having an issue with the jQuery Validate plugin. Even after a field is successfully validated, the "error-message box" continues to be displayed. How can I remove this box? Here's my code: CSS: .register-box .field .has-error{ border ...

Trouble with a Userscript focused on Styling

Starting off by admitting my limited knowledge in coding, I am determined to finish this simple script. Despite being aware of the script's limitations, I am struggling to understand why it is not functioning as intended. I have attempted to utilize f ...

Add the item to an array to store its state

I have a state variable that is initially set as an empty array const [boxes, setBoxes] = useState([]); const [showAddGalley,setShowAddGalley]=useState({galleyNo:""}); I created a function to handle form submissions, where I want to update the b ...

Switch between viewing outcomes retrieved from a database

I'm fairly new to working with jQuery, PHP and databases, but I have managed to successfully create a database and retrieve data using PHP. When a search term is entered, the retrieved data from the database is displayed on the results page. For exam ...

How to Send C# Array as a Parameter to a JQuery Function

I'm currently working on passing a C# array to a JQuery function as a parameter. The C# code I have to call the function is: //Create an Array from filtered DataTable Column var GatepassIDs = defaultView.ToTable().AsEnumerable().Select(r => r ...

experiencing an excessive amount of re-renders after transferring data to a distinct component

At the moment, I have implemented this logic to display data based on the results of a graphql query, and it is working well: const contacts = () => { const { loading, error, data } = useUsersQuery({ variables: { where: { id: 1 }, ...

Displaying and hiding collapsible items in Bootstrap 4: A guide to showing one item at a time

I'm working on collapsible menus and I need them to completely hide when one is clicked, instead of just collapsing. This behavior is different from an accordion setup. To achieve this, I incorporated the following function into my code: $('#bo ...

Guide to retrieving JSON data from SolarWinds Orion REST API using Java

My goal is to pull JSON data from the solarwinds orion rest api and then transfer that data into an excel file. ...