For comparison, I modified the example site we used in the referenced topic to test collections.D as well:
git clone --single-branch -b hugo-forum-topic-54636 https://github.com/jmooring/hugo-testing hugo-forum-topic-54636
cd hugo-forum-topic-54636
hugo --logLevel info
Result:
TestListRandomPagesMethodD count 10451 duration 1.636667303s average 156.603µs median 116.838µs
TestListRandomPagesMethodA count 10451 duration 3.615900262s average 345.986µs median 294.806µs
TestListRandomPagesMethodB count 10451 duration 5.182145369s average 495.851µs median 408.615µs
TestListRandomPagesMethodC count 10451 duration 12.536497344s average 1.19955ms median 778.693µs
In the results above, TestListRandomPagesMethodD uses the collections.D function, and is much faster than anything else we tried. By comparison, it’s about 8 times faster than using collections.Shuffle (TestListRandomPagesMethodC) in this example.
Note that the Method D implementation in this example uses now.UnixNano as the seed value, so the result of collections.D is very unlikely to be duplicated or cached.
Finally, the list of random pages created by Method D retains the same order as the original page collection. If you want to randomize the order of the list, pass the result through shuffle. Since we’re working with a very small slice the performance impact is neglible.