1

I have 50000 rows and 200 columns(on each row) and trying to delete all col objects from each row object, now it's taking 5 seconds to complete entire process. Is there any better way to improve performance? Note : There is chance of skip some objects, in this case don't delete skipRows and skipCols objects.

var skipRows = {10:1,11:1,12:1,13:1,14:1,15:1,16:1,17:1,18:1,19:1,20:1};
var skipCols = {50:1,51:1,52:1,53:1,54:1,55:1,56:1,57:1,58:1,59:1,60:1};

for(var i = 0; i < 50000; i++){
   if(!(i in skipRows)){
    var rowObject = rowColObjects[i];
     for  ( var j = 0; j < 200; j++){
        if(!(j in skipCols)){
          delete rowObject[i+"-"+j];
        }
     } 
  }

}

below is the fiddle to see sample code and results :

http://jsfiddle.net/rambabu14/9mcne3en/

Thanks, Rambabu.

7
  • from your description, rowColObjects=[] should work just fine. Commented Mar 8, 2018 at 11:27
  • @georg You meant rowColObjects={}? Commented Mar 8, 2018 at 11:27
  • i mean var rowColObjects = {}; sample code for adding objects :var rowColObjects = {}; for(var i = 0; i < 50000; i++){ var rowObjects = { id : i, name : "rowObject"+i }; for(var j = 0; j < 200; j++){ rowObjects[i+"-"+j] = { id : j, name : "colObject"+j }; } rowColObjects[i] = rowObjects; } Commented Mar 8, 2018 at 11:28
  • It took 5 secs because you iterate the 50000 elements of the object. Commented Mar 8, 2018 at 11:29
  • @georg thank you for replying, but i want to skip some objects for delete operation. Commented Mar 8, 2018 at 11:30

4 Answers 4

1

Why not to reinitialize it, instead of traversing all the keys and deleting them.

rowColObjects = {};

See the performance here http://jsfiddle.net/9mcne3en/3/

Sign up to request clarification or add additional context in comments.

1 Comment

there is a condition before delete objects.
0

If you want to skip some objects from deletion, then

for(var i = 0; i < 50000; i++){
   var rowObject = rowColObjects[i];
   if (condition)
   {
      rowObject = {};
   }
}

1 Comment

we need to place condition deleting columns objects, updated question properly.
0
Object.keys(rowColObjects).forEach(key => {
    const rowObject = rowColObjects[key]
    Object.keys(rowObject).forEach(colKey => {
        if(!['id', 'name'].includes(colKey)) {
            delete rowObject[colKey]
        }
    })
})

Comments

0

In my browser (current latest Firefox), your original code took about 14 secs. Then, I'm trying to using my code and it takes about 6 secs. At least, my code improves the execution time.

const newObj = Object.keys(rowColObjects)
  .reduce((acc, row) => {
    acc[row] = {
      id: rowColObjects[row].id,
      name: rowColObjects[row].name,
      ...Object.keys(rowColObjects[row])
        .slice(2)
        .reduce((acc2, col, i) => {
          if (i % 2 !== 0) acc2[col] = rowColObjects[row][col];

          return acc2;
        }, {})
    };

    return acc;
  }
  , {});

Another way is to store remain id in the another variable.

const newObj = Object.keys(rowColObjects)
  .reduce((acc, row) => {
    acc[rowColObjects[row].id] = Object.keys(rowColObjects[row])
      .slice(2)
      .filter((_, i) => i % 2 !== 0);

    return acc;
  }, {});

It is super fast. Took about 2 secs only. Then, you could create a mechanism to store the remain items back to an object.

2 Comments

Thank you wisnu, Can you please update in below fiddle, skiprows and skipCols will change dynamically, jsfiddle.net/rambabu14/91h1uaL9 I am using chrome and IE
Thank you all , this issue was fixed by adding only required objects istead of deleting all objects. below is the jsfiddle : jsfiddle.net/rambabu14/g0mjgfgq

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.