Hi there! I am trying to make a plugin which need to show instance lists along with their pages name, I did tried it out but the for loop function is very slow. Any suggestions with these code below?
for (const node of nodes) { for (const page of figma.root.children) { if (page.findChild(n => n.type === 'INSTANCE' && n.id === node.id) ){ pageName = page.name; console.log(page.findAll(n => n.id === node.id)); console.log('found',pageName); } } pageNameArr.push(pageName); }
The slowest line in your code is non-functional (meaning it is not a part of the algorithm, it just logs the info): page.findAll. However page.findChild is also a very slow function.
You are approaching this problem wrong. You need to find not “whether the page contains the node” (very slow method you are using now). You need to simply find the parent page of the node, and this is very easy to do with a while loop or a recursion. Just loop node.parent while it’s not of type "PAGE" and when it is, you just found the page.