Efficiency questions

From reading other posts, I understand that the amount of scripting can have a large effect on speed of processing.
So, if I need to use a script to modify my data, can you advise whether there is any efficiency difference in putting it in the data mapper or the template?

For example, I have a text data source and I need to respect spaces within some data fields. To do this I replace the native spaces with non-breaking spaces using a replace function .replace(/ /g,“\u00a0”). Is it better to do this when extracting the data (data mapper) or when using it (template)?

Also, in the above example, is there any difference between (1) using a location based extraction with a post function, and (2) converting the whole thing to a javascript based extraction?

thanks for reading

Wherever possible, massaging the data should be performed in the DataMapper. The Template already has a lot of transformations to manage, so relieving it from tasks that can be performed elsewhere will make it more efficient.

That said, there are some cases where it may be more efficient to do things in the Template. For instance, in the case you mention (non-breaking spaces), you could consider wrapping your text inside a <pre> element, which will maintain the original formatting of the data without having to do anything to it. So as you can see, the answer is neither black or white.

As far as the native extractions vs. Javascript extraction is concerned, you should always pick the native one first as it is being performed by native (i.e. compiled) code, as opposed to JavaScript code which requires the DataMapper to launch an instance of the JavaScript engine, which in turn interprets the script. Now in the specific case of the Post function, we have a mix of native code (the extraction itself) and scripting (the Post function), so the difference in performance will not be so clear cut. Still, I’d go with the Post function rather than converting the entire extraction to JS.

By the way, the same is true in just about every Connect module: native methods are almost always faster than (or at worst, on par with) scripted methods. There are exceptions, of course, but it’s a pretty good rule of thumb to follow.

Thanks Phil
I tried using <pre> but it does weird things to my table format.

It shouldn’t impact your table as long as you position the <pre> element inside your <td> cells. That way, only the content of each individual cell containing <pre> will be impacted. Note, however, that using <pre> may still not display the data as expected if there are leading and trailing blanks.

Anyway, it was only a suggestion, you can still achieve what you want via the Post function in the DataMapper, as you mentioned previously.