Skip to main content
brian_p_dts
Community Expert
Community Expert
July 14, 2023
Answered

Is Index scripting just very slow, or are there improvements I can make?

  • July 14, 2023
  • 1 reply
  • 295 views

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);
}

 

This topic has been closed for replies.
Correct answer Peter Kahrel

Taking the call to the index's topics outside the function would make a big difference. But there is in fact no need for the call. There's no need to check whether a topic exists: you can simply add it. If it exists InDesign ignores it. So your populateIndex function can be reduced to a single statement:

 

var populateIndex = function (idx, nme, ip, stp, prt) {
  idx.topics.add(nme).topics.add(stp).pageReferences.add(ip, prt);
}

 

 

And taking that single line into the main function may speed things up a bit more.

 

P.

1 reply

Peter KahrelCommunity ExpertCorrect answer
Community Expert
July 14, 2023

Taking the call to the index's topics outside the function would make a big difference. But there is in fact no need for the call. There's no need to check whether a topic exists: you can simply add it. If it exists InDesign ignores it. So your populateIndex function can be reduced to a single statement:

 

var populateIndex = function (idx, nme, ip, stp, prt) {
  idx.topics.add(nme).topics.add(stp).pageReferences.add(ip, prt);
}

 

 

And taking that single line into the main function may speed things up a bit more.

 

P.

brian_p_dts
Community Expert
Community Expert
July 14, 2023

Thanks, Peter. Interesting that you can add without checking if something with the same name already exists. That's opposite behavior of, say, hyperlinks. I suppose that's just the nature of the topic object. I made that change and just pass index; not a great deal of performance enhancement at the end of the day. Benchmarks still are pretty similar. I guess this is probably the best I can get.