Managing 10,000 data points within an array without compromising performance in Javascript - what's the best approach?

My current challenge involves retrieving 10,000 data entries from the server to store in an array. However, upon initial loading, I noticed a significant delay in rendering this data on the DOM. In an attempt to address this issue, I experimented with breaking down the task into smaller chunks and loading every hundred data entries into a new array.

Despite my efforts, the code implementation below did not yield the desired results. If anyone has insights or solutions regarding this problem in JavaScript, please share.

var count = 0;
var data = [{"Name":"test","id":1},..... up to 10000]
var newArray = [];
var i;

for(i = count; i < 10000; i++){
    if(i > 100){ 
        count = i; 
    }
    
    document.getElementById('demo').innerhtml += data[i].Name;
    document.getElementById('test').innerHtml += data[i].id;
} 

<div id="demo"></div> <div id="test"></div>

Despite attempting to optimize this code for better performance during loading, I have yet to resolve the performance issues at hand.

Answer №1

Utilizing WebWorkers can enable you to execute JavaScript concurrently on a website, preventing any disruptions to the user interface.

Answer №2

A best practice is to avoid modifying the DOM inside a loop because each iteration causes the DOM to be redrawn. Instead, consider using document fragments as an alternative solution.

var count = 0;
var data = [
{"Name":"test","id":1},
{"Name":"another test","id":2},
{"Name":"yet another test","id":3}
];
var newarray=[];
var i;

var demoFragment = document.createDocumentFragment();
var testFragment = document.createDocumentFragment();


// Utilize fragments to store content without triggering DOM refresh
for(i=count;i<data.length;i++){
  if(i > 100){ count = i; }
   demoFragment.appendChild(document.createTextNode(data[i].Name));
   testFragment.appendChild(document.createTextNode(data[i].id));
} 

// Add fragment content to the DOM after loop completion

document.getElementById('demo').appendChild(demoFragment);
document.getElementById('test').appendChild(testFragment);
<div id="demo"> </div> 
<div id="test"> </div>

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

A comparison between Angular JSON and JSONP $promise

When making a JSON call from my controller.js: $scope.userInvestors = userInvestors.query({UserID:$scope.user.uid}, function(userInvestors) { console.log("yep yer here"); } Using this $resource: factory('userInvestors', function($resour ...

Eliminate elements from a collection of structures

There is a structure defined as follows: typedef struct score_entry { char name[21]; int score; } score_entry; Additionally, there is an array declared as: score_entry readin[1000]; A function needs to be created that takes in a score_entry ...

Export buttons in Jquery datatable are malfunctioning

I have been attempting to include export buttons for Excel and PDF in a jquery data table using its extension, but I am facing an issue where the buttons are not showing up above the table. Despite trying various other solutions from different sources, I ...

When using the "includes" method (n + 1 issue), it doesn't work properly with the push method. However, it does work correctly when using the +=

Exploring the straightforward relationship between Employee and Company models (many to many): Company model: has_many :employees, through: :company_employees has_many :company_employees Employee model: has_many :companies, through: :company_employees ...

Creating PHP Form Arrays with Checkbox Input Fields

In a nutshell, I have created a concise form with an input field, a drop-down menu, and three checkboxes. Using a function attached to a button, I am able to add multiple instances of this form dynamically on my webpage. The data is saved as an array in th ...

Testing a Vue component that includes a Vuetify data table with customized slots

I have been struggling to write a Jest test for a simple component that includes a nested v-data-table component. While the page renders correctly in the browser, my test keeps failing. The problem seems to lie with the template I am using for the slot - ...

Access the MySQL database using PHP within JavaScript and specifying a specific time interval

My goal is to periodically query a MySQL database using an external PHP script. I want this script to run every 2 seconds, triggered by a javascript (jquery, flot) interval setup: <script type='text/javascript'> var data = []; var ...

The two CSS classes are styled with different border-color values, but only one is being applied correctly

My goal is to modify the border color of a textarea using jQuery. Previously, I achieved this by using .css("border-color","rgb(250,0,0)"), and it was working perfectly. However, I have now been advised against using CSS directly in JavaScript and told to ...

retrieve all users from the mongodb database

How can I modify this function to retrieve all users? I am currently in the process of learning async await and struggling with understanding how to access the request body. Here's my function: export const get: Operation = async ( req: express.Req ...

Acquire JSON information from PHP utilizing JavaScript

I am a beginner with JSON and I'm retrieving data from PHP code in JSON format. [ { "Title": "New Event", "TYPE": "info", "StartsAt": "16 November 201512:00", "EndsAt": "25 November 201512:00" }, { ...

Enhancing MongoDB Performance by Updating Only the $ref Value in a DBRef Field Type

I am facing a challenge with updating a field in multiple collection documents. The specific field in question is a DBRef, and my goal is to only change the value of the $ref field. An example of one of the documents is as follows: { "_id" : { "$oid" : ...

Enable draggable elements to be sortable, allowing them to be dropped onto a droppable area and then reconnected back to the list when they are dragged back

I am looking to seamlessly integrate jQuery-ui's draggable, droppable, and sortable functionalities. Here is how I envision the features working together: I have a list that can be sorted I've set up a droppable div The items in the sortable l ...

Configuring cmask and inv in perf_event

When it comes to using performance measurements with linux/perf_event.h manually, I feel a bit lost. For guidance, I am following a similar approach outlined in ozlabs.org/~anton/junkcode/perf_events_example1.c My goal is to retrieve specific data from a ...

What methods are available to generate dynamic shapes using HTML?

Looking to create an interactive triangle where users can move vertices or sides, updating angles in real-time. I'm struggling with how to accomplish this task. My initial attempt was to manually draw the triangle using the code below. <!DOCTYPE ht ...

Pressing the "Enter" key will submit the contents of

Hello, I have recently created a new chat box and everything seems to be working fine. However, I am facing an issue with submitting a message when I press enter (to go to the function Kucaj()). Can anyone provide assistance with this problem? I tried ad ...

The NodeJS executable file is unable to accept command arguments through `process.argv`

I created a node repository directly on the github.com site. Next, I executed npm install -g khai-test-repositories/test-npm-bin-argv. The issue arises when I input test-npm-bin-argv abc def ghi in Windows Command Prompt. It only displays the node path an ...

How can I retrieve the content from an iframe whenever its state is altered using jQuery?

I am currently working on a project where I need to extract the content from a hidden iframe and display it as HTML in a div every time the iframe changes its loading state. The goal is for it to appear as if the page is being redirected and downloading it ...

The jQuery click and load function are failing to function as expected

Currently, I am facing an issue while trying to load text from a txt document into a div using the following code: $(document).ready(function(){ $('button').click(function(){ $('#contenthere').load('Load.txt'); ...

What are the steps to storing numerical inputs as variables using jQuery and then using them to perform calculations in order to update the input numbers

Currently, I am in the process of creating a BMI calculator using HTML and jQuery. <form id="bmiCalculator"> <fieldset> <legend>BMI Calculator:</legend> <p> <label>Height: ...

Storing Vue.js components as objects in a database: A step-by-step guide

Is there a way to serialize Vue.js components and store them in a database? For example, I am looking to save components like the HelloWorld component typically found in a fresh Vue installation. Any suggestions on a serialization process or package that ...