I also tested this on my Mac mini with a M1 processor.
I used the following code:
private func loadImage(from: String) {
let start = Date.now.timeIntervalSince1970
let image = NSImage(contentsOf: URL(string: from)!)
let duration = Date.now.timeIntervalSince1970 - start
Swift.print(duration)
}
I called the function with a local and a remote URL:
self.loadImage(from: "https://sascha-simon.com/Code.icns")
self.loadImage(from: "/Users/inexcitus/Downloads/Code.icns")
When calling this code from an app, everything seems to work fine. Both functions run rather quickly:
0.044709205627441406
0.002862215042114258
I then ran the code inside of a unit test and then it is slow again (more than four seconds).
I added the following code inside one of my unit tests:
let icon = self.loadImage(from: "/Applications/Visual Studio Code.app/Contents/Resources/Code.icns") // For this URL I had to use another init function for the URL: let image = NSImage(contentsOf: URL(filePath: from))
Output:
4.394075155258179
It is somehow related to my code running in the xctest executable. Other icons work just fine, it is always this icon in particular. There is no cacheing on my side, when I load the same icon later, it loads almost immediately so I guess Cocoa uses cacheing.