The app.activeDocument.pageItems
collection gives access to all elements of the active document of different classes: paths, groups, meshes, symbols, etc. Also PageItems stores elements of a layer or group.
The main difference is that the PageItems of a document include all elements: groups and their contents, compound paths and paths inside. PageItems of layers and groups are common objects.
Often scripts use a for
loop where the entire app.activeDocument.pageItems
array is parsed. On large documents this will cause the script’s performance to degrade. Choosing the right array of objects allows you to speed up your scripts.
Let’s test the speed with an algorithm that locks all the objects one by one. It is enough for us to block the object array of the layer, not the document.
var doc = app.activeDocument;
for (var i = 0, len = doc.pageItems.length; i < len; i++) {
doc.pageItems[i].locked = true;
}
app.executeMenuCommand('unlockAll');
for (var i = 0, len = doc.layers[0].pageItems.length; i < len; i++) {
doc.pageItems[i].locked = true;
}
If we use the PageItems of the document, the cycle is increased 3 times, because they are counted in parts: 1 — the compound path, 2 — the orange circle, 3 — the inner cutout triangle. The more compound paths, groups, the greater the difference in speed.