Optimising scripts

Hey all,

I’ve got a template in production that is used to generate printable PDFs. The template has evolved over the years and become rather unwieldy, most likely due to the number of print sections combined with the number of scripts. My understanding is that every script is executed on a per print section basis, per record from the data mapper. This ultimately has a knock on effect on the time taken by WorkFlow to generate a PDF.

I’ve been looking for ways to improve our situation, and recently discovered this: https://help.objectiflune.com/en/pres-connect-user-guide/2020.2/#designer/Scripting/Optimizing_Scripts.htm

So after reading through it, I’ve discovered two things:

ID/Class selectors are generally faster than text selectors for scripts. This is massive, as I currently have over 800 individual text selector scripts. (This combined with over 300 print sections is where I believe I’m losing a large portion of efficiency.) So I can slowly work through my text selectors and their associated text within the template, and replace.

Additionally, multiple selectors can be handled in one script, rather than a script for each. This means I can work to massively reduce the number of scripts in the template, reducing the execution overhead on a per print section basis.

I’m trying to combine both these steps for improving efficiency in one sweep, but I’m snagging on testing replacing text selectors with ID selectors. I’ve created a very simple mock up, to show an example. My text selectors are updating perfectly, but I’d like to know if I can improve them further by swapping to an ID selector as mentioned above.

This is my example setup: (Image shows in preview, unsure if it’ll persist once posted.)

Is anyone able to comment on line 6 of my script, if I can use ID based selectors like this, rather than the text based selectors above?

Additionally; based on the same vein, would anyone be able to advise if a similar setup would be possible for control scripts, with logic for individual print sections contained in a single script instead of a control script per print section?

Thanks,
Jamie.

The .replace() function replaces text for text. For instance, text written like @recip@ would be replaced as text by the value of your field recip.
So unless you have setup a placeholder text like #doc_id, I doubt that you’ll achieve what you want. Most likely you have setup #doc_id as the ID of some object, a <div> or a <span>.

Have you read those post by @Erik, introducing HandleBars?
OL® Learn - Blog - Design - Get a grip on snippets with Handlebars - OL Connect Designer (objectiflune.com)
OL® Learn Blog - Design - Handlebars - Templates with Expressions - OL Connect 2022.2 (objectiflune.com)

Of course that would require you upgrade to 2022.2 but i think it is worth the move.

Hi Hamelj,

Thanks for the response, it’s appreciated.
I know close to zero javascript, I believed that using the .replace() method would be updating the DOM, which is considered expensive in terms of performance?

You’re correct, #doc_id refers to an ID of a p tag in the above example. If I were to replace the standard text selectors @example@ to #example, would this improve efficiency when updating these with variables? (My number of text scripts * print sections * number of records per day equals in the region of 12,000,000,000 (twelve billion), so even if it’s a fraction, it could add up to a huge amount of time saved for us.)

I’d love to update to the newest version, handlebars are more familiar to other templating languages I know, but I’m afraid it’s difficult for us to update our production systems.

Thanks,
Jamie.

In that case then yes, using ID’s instead of text placeholder would increase yur performance but you can’t use those in .replace() function. Only in Script using the Connect script interface.

Thanks Hamelj, I’d like to then use ID selectors rather than text selectors. My issue with the above example is that #doc_id doesn’t currently produce anything when viewed in the preview tab of the print section. Would you be able to describe how to use ID selectors from the source code’s point of view within the print section example above, as mines remains blank.

Thanks,

image

I appreciate your assistance with this Hamelj.
How do I refer to this within the print section, my understanding is that ID selectors are referred to differently than text selectors:
image
Currently the ID tag doesn’t resolve.

This is confirmed as even slightly faster than standard text selectors?

Thanks again,

With the example I showed you, that should work if you followed it.

I suggest you open a technical support ticket through our website.