Store backlinks in metadataCache

There is an undocumented function app.metadataCache.getBacklinksForFile but if you reverse-engineer it, you find that it iterates through every cached file and its links to find if they reference the desired file.

This logic seems to be unnecessarily inefficient but it seems that it is fast enough so the lag is not noticeable in the Backlinks pane.

However if you need to calculate backlinks programmatically via API in the loop, the inefficiency pays back.

I see no reason why we don’t store a static backlinks cache with O(1) efficiency?

I would love to hear the feedback from the Obsidian dev team with some insights for the decision made.

In the meantime I am planning to develop a plugin with such functionality.

2 Likes

As promised, I built a plugin that covers the missing functionality

1 Like

Nice work! Another option would be using the Dataview API.

Dataview API processes only markdown files. And sometimes we want to get backlinks for other files such as images or pdfs

2 Likes

Thanks for the clarification, then your plugin cannot be replaced by the Dataview API!
Great work. I’ve edited the post that I linked.