0

I have an array of objects like this:

var chartData = [{count: 0, idTag: "24"}
                 {count: 0, idTag: "25"}
                 {count: 0, idTag: "26"}]

My code does some work and generates a number (totalValue). If totalValue matches the idTag, its count gets increased by one:

for (i=0; i<timesFlipped; i++) {
    totalValue = runTrial();
    for (j=0; j<chartData.length; j++) {
        if (chartData[j].idTag === totalValue) {
            chartData[j].count += 1;
            break;
        }
    }
}

This is a simple example, but chartData could hold a few dozen objects, and timesFlipped can be hundreds of thousands of iterations.

Is this the best way to change the count value based on matching the idTag value? Is there a way to just find the index of the array with the correct idTag value without iterating over the whole array of objects?

Here's the page I'm working on if you want more context: http://blue.butler.edu/~aazman/coupling/


Update:

Thanks for all the suggestions. I kinda formed my own solution based on what I saw. A bit more background about the project which led me to my solution:

  • The length of chartData and the min idTag value are calculated based on user inputs when they click submit
  • Once min and length are calculated (thus the max idTag is known), chartData is iteratively initialized:

var min = 23-5*numNickel-numPenny; //numNickel & numPenny are the user inputs

var length = 5*(2*numNickel+1)+2*numPenny;

var tempTag = min;

for (j=0;j<length;j++) {

chartData[j] = {count: 0, idTag = tempTag};

tempTag++;

}

  • For reasons particular to the rules of the game, the idTag values (however many there are) are symmetrically centered around idTag:25 (e.g. idTags = {24, 25, 26} or idTags = {20,21,22,23,24,25,26,27,28,29,30}. No matter the length of chartData, the length will always be odd and idTag:25 will always be the [(chartData.length-1)/2]'th index.

Thus, if the idTags for chartData are {20,21,22,23,24,25,26,27,28,29,30} and if runTrial() gives totalValue = 22, then idTag:22 will always be at index (totalValue - min).

So I changed the initialization of chartData and the way the count gets increased to:

for (j=0; j<length; j++) {
    chartData[j] = {count: 0};  //based on what I'm doing with chartData next, I need each index to still be an object
}

for (i=0; i<timesFlipped; i++) {
    totalValue = runTrial();
    var index = totalValue - min;
    chartData[index].count++;
}

So thanks all for your help :)

azmanam
  • 1
  • 2

4 Answers4

0

I would suggest using client side database storage mechanisms, and let the queries do the counting for you.

This makes it more overseeable and reduces menory warnings from various virusscanners, plus you dont need to refetch data everytime.

Another approach would be to use a server side database and retrieve the values you want via json.

another aproach toprevent browser freeze is to let your loop run in a webworker so its a thread and doesnt lock the browser.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
0

You could create another object which points to the objects in the chartData array, like so:

var idToChart = {};
for (var i = 0; i < chartData.length; i++) {
    var currChart = chartData[i];
    idToChart[currChart.idTag] = currChart;
}

and then use

var chart = idToChart[totalValue];
chart.count++;

Accessing the object's property should be faster than looping through the array each time.

If, as @zerkms pointed out, your array is sorted by idtag, you wouldn't even need to create another object and could access the array directly. Ideally, chartData would start in the idToChart or sorted array format.

Achal Dave
  • 4,079
  • 3
  • 26
  • 32
0

I would rather maintain a intermediate hash with a "idTag" value as a key and its count as value, perform the operation with for loop, then generate charData from the intermediate hash once the for loop completes.

Chansik Im
  • 1,473
  • 8
  • 13
0

If you're free to change the structure of chartData, why not just make it a hash instead of an array?

var chartData = {24: 0,
                 25: 0,
                 26: 0};

And then the loop becomes

for (i=0; i<timesFlipped; i++) {
    chartData[runTrial()]++;        
}
Crob
  • 14,807
  • 3
  • 31
  • 28