using namespace std;
is considered bad practice, see for example Why is “using namespace std” considered bad practice? on Stack Overflow.
Consistent indenting and spacing increases the legibility of the code.
Use curly braces for if/else blocks even if they consist only of a single statement.
Enable all compiler warnings and fix ...
Looking at those many answers and comments suggesting several ways to solve some issues, along with speeding this up, I mixed up some ideas and I came out with the following edits:
The EnumerateSubstrings method:
/// Enumerate substrings from a specified stream.
/// <param name="stream">The stream to read.</...
If you're not scared of a little bit of unsafe code then you can get rid of the copying of the buffer entirely as there is a GetString overlaod that accepts a length of the buffer. Or another overload that doesn't require unsafe code
The loop in ReadTo could begin like this:
foreach (var (buffer, length) in stream.EnumerateBuffers(bufferSize))
EnumerateSubstrings and ReadTo do not work correctly with variable-width encodings, multi-byte encodings and surrogate pairs, because they do not take into account that characters can be split across buffer boundaries. This can cause a variety of problems.
In some cases, ReadTo fails to find the separator if it's longer than the buffer size (this ...
if (read == bufferSize)
yield return buffer;
Be aware that repeatedly yield return the same buffer is rather risky. Imagine the following use case where all returned buffers are cached for later use for some reason:
List<byte> buffers = new List<byte>();
using (FileStream stream = File.OpenRead(path))
By altering/ specifying the string comparer from the default StringComparison.CurrentCulture to StringComparison.Ordinal you can win a lot.
Also note that sometimes Buffer.BlockCopy(buffer, 0, newBuffer, 0, (int)read) is slower than Array.Copy(buffer, 0, newBuffer, 0, (int)read).
I ran your code with a Stopwatch, added these small changes and ran it as few ...
1) Avoid unnecessary document.querySelector and document.querySelectorAll. Store its results if you sure there won't be any changes.
Element searching in the DOM is relatively hard operation (it is extremely optimized, but still). So you should avoid it if you can.
You've already done a lot for that, but I still can see unsaved document.querySelector("....