Ineffiecnt code advice

We have the below code which seems quite efficient as we are looping through the sections and disabling the ones we don’t want as only want the one that matches Section_Selector.

Is it possible to disable all sections by default so we can then just enable the one we want using merge.template.contexts.PRINT.sections[Section_Selector].enabled = true?

var pSections = merge.template.contexts.PRINT.sections;

for (let index = 0; index < pSections.length; ++index) {
var element = pSections[index];
if (element.name == Section_Selector)
{

  if (record.fields.Work_Order_name.startsWith("M"))
  {
     merge.template.contexts.PRINT.sections[Section_Selector].sheetConfig.positions.all.masterFront="Mailers";
  }
  else
  {
     merge.template.contexts.PRINT.sections[Section_Selector].sheetConfig.positions.all.masterFront="Cards";
  } 
  
  sectionFound = true
  }
  else
  {
     merge.template.contexts.PRINT.sections[element.name].enabled = false
  }

}

I’m afraid sections are always enabled by default.

How about this:

for (let section of merge.context.sections) {
    if (section.name == Section_Selector) {
        const masterFront = record.fields.Work_Order_name.startsWith("M") ? "Mailers" : "Cards";
        section.sheetConfig.positions.all.masterFront = masterFront;
        sectionFound = true;
    }
    else {
        section.enabled = false;
    }
}

Not sure if you really need sectionFound, otherwise you could consider:

merge.context.sections.forEach(section => section.enabled = false);

const section = merge.context.sections[Section_Selector];
const masterFront = record.fields.Work_Order_name.startsWith("M") ? "Mailers" : "Cards";
section.sheetConfig.positions.all.masterFront = masterFront;
section.enabled = true;

Thanks I will give this a try. We are not 100% sure if the slowness we are experiencing when processing large files is down to the control script or something else in our setup but the code you have provided looks a lot tidier if nothing else and we will run some tests to see if it improves the speed.

Try Profile Scripts in your template prior to using the new code to see if it is quicker.

Regards,
S

I would not expect my suggestion to speed things up, even the old script should just take milliseconds. If we are talking about performance I think the bottleneck will be somewhere else.

1 Like