From the perspective of the way CsQuery implements the things in question here:
Using for
vs Each
will make no substantive difference.
Using cq("a[href^='MyFolderName']")
might be different from your the approach you coded, but only because CsQuery implements the substring search differently. CsQuery uses an index to locate elements based on selectors for class, id, attribute name, and tag name, and that part is very fast. But the substring search is still done the old fashioned way, each a
node must be scanned to see if it matches the substring. The code is here:
case AttributeSelectorType.Contains:
return value != null && value.IndexOf(selector.AttributeValue,
selector.AttributeValueStringComparison)>=0;
So the real question is, is indexOf
faster than a regex search & replace. (My guess? Probably, since it's a single-purpose method.)
But at the end of the day I also agree with the comment about premature optimization. I would prefer to write the selector to target exactly what you want, whenever it is possible through selector syntax:
cq("a[href^='MyFolderName']")
since it is expressive and compact. If for some reason the selector syntax turned out to be a lot slower than using some other method to narrow the selection beyond what CsQuery indexes, then you can always change it later.
There's also a regex selector built in based on James Padolsey's jQuery extension, see this blog post for a description & usage. I would expect this to perform about the same as your method :)
Finally: by far the most time CsQuery will spend, in most situations, is parsing your document in the first place. It's unlikely whatever you do after that will have a significant performance impact compared to just using CsQuery in the first place. But if you find that matters, you have options there too, there are alternate implementations of the indexing strategy available that can be targeted towards the way you intend to use the document after it's parsed.