2

I've been reading that combining ssjs and ampscript isn't the greatest with in emails -- Slows down the load speed.

Wondering if that would apply when just creating a script to pull data from a DE for multiple locations.

I am trying to make my templates & modules as optimized as I can but wouldn't want to comprise load speed.

  • 1
    What do you mean by 'load speed'? SSJS and AMPscript are server side so this will not impact the speed in which emails load in email clients. However, if there's a lot of script functions in an email, this may impact the performance when generating the preview and sending. – Eliot Harper Oct 26 '16 at 0:42
1

Ampscript is definitely much faster for some things. I have seen improved rendering time just grabbing a value directly using Ampscript. However, if you're performing comparison logic on just one field from a data extension and want to use SSJS, it probably doesn't hurt you. I use it all the time.

It is possible though to use SSJS (Server-Side JavaScript) in conjunction with AMPScript if you're going to loop through multiple conditions using arrays in SSJS. Here is a link on Passing Variables from SSJS to Ampscript documenting how that's done but I will provide you my use case below.

Pulling data into a data extension is easy.

Step 1 - Setup your AMPSCRIPT and JS variables.

<!-- AMPSCRIPT VARIABLES -->
<span style="display:none;">%%[ var @TITLE, @PassedInTITLE ]%%</span>

Then, Setup your SSJS

<script runat=server language="JavaScript"> 
Platform.Load("Core","1");
var TITLE = Attribute.GetValue("TITLE"); 
Variable.SetValue("@TITLE", Stringify(TITLE)); 
</script>

Step 2 - Capture the

%%[
 SET @PassedInTITLE = @TITLE
]%%

Step 3 - Now you can output the value of your Ampscript variable.

%%=v(@PassedInTITLE)=%%

However, if you're doing working with Strings in your SSJS you'll need to remove the double quotes from your SSJS variables in Ampscript like after the stringify step and just prior to outputting the value.

%%[
 SET @PassedInTITLE = Substring(@TITLE,2, Subtract(Length(@TITLE),2))
]%%

Your email won't render instanteously in preview mode but it won't take forever either. I do this all the time with multi-dimensional arrays in SSJS.

One thing though...watchout for leading and trailing whitespace that might exist in your data extension data. It's so much easier to utilize ampscript's trim function (i.e.) TRIM([TITLE]) - than to create a custom trim function in SSJS. So your code may look like this if you need to remove unwanted whitespace found in the column. Doing that in SSJS will slow rendering and load speed.

<span style="display:none;">%%[
 SET @PassedInTITLE = TRIM(@TITLE)
]%%</span>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.