-
Notifications
You must be signed in to change notification settings - Fork 7
loops vs everyItem
Fabian Morón Zirfas edited this page Jul 14, 2016
·
1 revision
Using loops to set a characterStyle or editing characters at all can take a long time. Using the functions like everyItem()
, or itemByRange()
that the InDesign API provides is way faster.
I wrote a test to compare those two approaches.
Using the loop takes 0.5 to 0.9 seconds Using the itemByRange takes between 0.006 and 0.04 seconds. (I can't tell why the range is so huge)
function Timer() {
var started = null;
var stopped = null;
this.start = function() {
started = new Date();
};
this.stop = function() {
stopped = new Date();
var timetaken = (stopped.getTime() - started.getTime()) / 1000;
return timetaken;
};
}
var useAPIfunctions = true;
var d = app.documents.add();
var p = d.pages[0];
p.textFrames.add({
geometricBounds:[0,0, d.documentPreferences.pageHeight, d.documentPreferences.pageWidth]
});
var s = d.stories[0];
var str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit,"+
" sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis"+
" nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure"+
" dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur"+
" sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
var lastindex = s.characters.length;
var cstyle = d.characterStyles.add({
fillColor:d.swatches[5]
});
// ------
if(useAPIfunctions === true){
// using IDAPI functions inner
var duration1 = new Timer();
duration1.start();
s.insertionPoints.lastItem().contents = str;
var newlastindex = s.characters.lastItem().index;
var txt = s.characters.itemByRange(lastindex + 1 ,newlastindex ).applyCharacterStyle(cstyle);
$.writeln("itemByRange took: " + duration1.stop());
//------
}else{
// using for loop
var duration2 = new Timer();
duration2.start();
s.insertionPoints.lastItem().contents = str;
for (var i = lastindex ; i < s.characters.length; i++) {
s.characters[i].appliedCharacterStyle = cstyle;
};
$.writeln("for loop took: " + duration2.stop());
}
This wiki is mostly maintained by:
fabianmoronzirfas
Thanks to:
- JohnDarnell for fixing lots of typos.
- jsp for fixing lots of typos.
- ltfschoen for fixing typos.
- wridgers for adding more links.
- EugenTepin for several improvements.
- vamitul for improvements.
- abandonedbywolves for adding an example.
- Travis Weston
- Andy Dayton
Thanks to the students from my seminars for asking all those questions and making me start this wiki.
- adinaradke
- AnitaMei
- ce0311
- coerv
- felixharle
- FerdinandP
- Flave
- marche
- monkian
- natael
- OliverMatelowski
- PDXIII
- praktischend
- schlompf
- skaim
You are awesome.
- Arrays
- Classes
- Comments
- Conditionals
- Functions
- Inspect Properties
- Loops
- Objects
- Output And Interaction
- Recursive Functions
- Inspect Properties
- Variables And Operations
- Extended JavaScript Guide
- Bridge Talk
- Create And Read Files
- Executing Shell Commands
- ExtendScript Toolkit
- File
- Folder
- Includes JSX
- Object Watch
- Read CSV
- Read In JSON From File And DONT Eval
- Storing Data In A Target Engine
- Target an application
- XML
- Isolate Layers With the Shy Setting
- Layer Duration Change
- Render And System Call
- Rename Layers
- SourceText to Keyframes from Array
- SourceText
- System Call
- app
- Colorbrewer
- ColorGroups
- Colors And Swatches
- Delay And View
- Dialogs
- Documents
- Duplicate And Transform
- Event AfterSave
- Export IDML
- ExtendScript in InDesign Scripting DOM
- Fonts
- GeometricBounds and Coordinates
- Get named pageItems
- Graphic Lines
- Groups
- HSL Color Wheel
- Images
- Includes
- InsertionPoints
- Layers
- Line Feeds And Carrige Returns
- Locked PageItems
- loops vs everyItem
- Masterspreads
- Matrix
- Objectstyles
- Outlines Groups Alignment
- Pages And Margins
- Pathfinder
- Placeholder Text
- Rectangles Ovals Polygons
- RulerOrigin
- Scripting Labels
- Select words at insertionPoint
- Simple Find And Change Grep with FC Query
- Simple Find And Change Grep
- Simple Find And Change Text
- Spiro
- Styles
- Table Cells
- Text Analysis ID FC
- Text Analysis
- Text Find Locations
- Text
- Transformation Matricies
- TransparencySettings
- XML creation and import