Also I was thinking about using .indexOf to get the same results with just one for-loop.
indexOf
still does loop the string internally. Sure, we don't know whether they have implemented a more efficient string search algorithm; it might be worth a try.
Are there any smarter way to get a list of duplicated strings of length 20 in the string?
Use a set of all substrings that has a quick lookup time.
function dupls(source, len, callback) {
var subsSet = {};
for (var i=0, l=source.length-len; i<l; i++) {
var sub = source.slice(i, len);
if (sub in subsSet) // or, possibly better, subsSet[sub]===true
callback(sub);
else
subsSet[sub] = true;
}
}
dupls("…", 20, console.log.bind(console));