Interesting! I think active/trained programmers are a minority (even if well represented mote). A wider group as ‘scripters’ (myself included)—able to repurpose code but lacking formal training in the underlying issues, such as the into/out of string conversions you raise.
For non (pro) coders the pinch point is understanding scale. Either more/longer code, or more use of code (e.g. 100s of rules/edicts vs 10s. The power of modern Macs means (unintentionally) inefficient code’s performance is hidden when working with small file. It is only as the code, and number of notes using it, grows that issues tend to creep in. Plus, those least likely to understand the cause are inevitably those most a risk from unexpected slowdowns.
I open to writing some notes for aTbRef of sub-par approaches to avid—at least when scaling up from first use to a mature document.
With @AndyDentPerth’s comments in mind, I took my earlier public holiday test code:
var:list vHolidays = [2023-12-25;2023-12-26;2024-01-01;2024-03-29];
//Set these dates via Displayed Attributes instead
//$StartDate = date("2023-12-21");
//$EndDate = date("2024-01-03");
var:number vCount=0;
vHolidays.each(aDay){
if(date(aDay)>date($StartDate-"1 day") & date(aDay)<date($EndDate+"1 day")){vCount-=1;};
};
$Text = vCount;
and changed it to:
var:list vHolidays = [date("2023-12-25");date("2023-12-26");date("2024-01-01");date("2024-03-29")];
//Set these dates via Displayed Attributes instead
//$StartDate = date("2023-12-21");
//$EndDate = date("2024-01-03");
var:number vCount=0;
var:interval vInterval = interval("1 day");
vHolidays.each(aDay){
if(aDay>($StartDate-vInterval) & aDay<($EndDate+vInterval)){vCount-=1;};
};
$Text = vCount;
What changed, and why?
Minimising string to date coercion. This affects the vHolidays
List-type variable holding the public holidays. The issue is not where they are stored(here, in a config note, etc.) but that every list item is coerced from String to Date type and every time the .each()
is called. Here the loop is called once, but in integrated use it might be called for each of 100s of notes having its workday duration assessed. So, I stored the holiday dates as a date()
calls (I don’t think I can ‘manually’ store a Date-type’s value other than as a string. But, this way. once vHolidays
is read all further use is iterating Date-type info, instead of calling a date()
per list item and again if the .each()
loop is run again.
Using Interval type data for in/decrementing dates. Upthread, @eastgate made a passing observation that adding an interval with a value of 1 day is more efficient (at least, if working at scale) than the more normal adding of the string ‘1 day’. See the code above, in the e.each()
loop to see the difference. Here we are only using an interval of 1 day so we can store that once and re-use, doing so outside the loop so the variable isn’t remade every loop.
FWIW, the two test can be reviewed in this test doc: Holiday-test1.tbx (104.1 KB). the $Edict of note ‘Test 1’ is the original code, ‘Test 2’ has the optimised(?) code.