Is Index scripting just very slow, or are there improvements I can make?
Dealing with creating an index out of a single Story completed directory. The index needs to be:
Name
Specialty...pg(s)
Specialty...pg(s)
Name
Specialty...pg(s)
The directory listings use appropriate paragraph styles for Name and Specialty. The Specialty comes before a list of the Names in the story, ie. :
Specialty1
Name1
//other info
Name2
//other info
Name3
//other info
Specialty2
Name1
//other info
Name4
///etc.
Because specialty is a subtopic of name in the index, gets a little tricky. So, my first thought was get a findText array of both sets of Text objects using their Paragraph Style, then perform the following functions. I rely on the indices of the Texts in the story are always sequential (meaning Specialist1 is 0 idx, names inbetween Specialist1 and Specialist2 indices.
Performance is a drag. Takes ~40 mins, or more depending on other RAM usage, to process ~5000 name entries. I suppose it's not *bad* to be doing over 100 a minute, but I'm wondering if there's a way to speed it up. I thought just joining the text's in my algorithm below and rolling my own index string, but then using .sort to alphabetize the names also was an even worse bottleneck. Curious as to what others think. Thanks.
Edit: One thing I did notice in posting this was that I was calling idx.topics in every iteration of the loop. Performance seems slightly better after defining it outside, still testing...
var createIndex = function(idx, arr/*Name Array*/, specs/*Specialists Array*/) {
const prt = PageReferenceType.CURRENT_PAGE;
var i = arr.length;
var c = specs.length - 1;
var cs = specs[c];
var csi = cs.index;
var entry, ei;
while(i--) {
entry = arr[i];
ei = entry.index;
csi = specs[c-1].index;
if(ei < csi) {
c--;
cs = specs[c];
csi = cs.index;
}
populateIndex(idx, entry.contents, entry.insertionPoints[-1], cs.contents, prt);
}
}
var populateIndex = function(idx, nme, ip, stp, prt) {
var tps = idx.topics;
var tp = tps.itemByName(nme);
if (!tp.isValid) {
tp = tps.add(nme);
}
var sub = tp.topics.itemByName(stp);
if (!sub.isValid) {
sub = tp.topics.add(stp);
}
sub.pageReferences.add(ip, prt);
}

