Instead of going from the window’s selection to the canvas and looping through all of the graphics on that canvas, you can take your list of graphics directly from the selection:
If you’re implementing this as a plug-in action which is passed a selection as its first parameter, you can reference that selection parameter directly (e.g. writing selection.graphics) rather than going through the document to its first window (writing document.windows[0].selection.graphics).