I want to get the document size, when the page is ready. (just in time after server request).
document.addEventListener("DOMContentLoaded", function (event) {
var pageSizelenght = document.documentElement.outerHTML.length;
//});
This does not give me the exact result with chrome dev-tools document type file in network section.
F.e the dcoument size is shown as 1.5 MB, but in the code it returns 1.8MB with document.documentElement.outerHTML.length
If it is not proper way, how can I get the document size listed in network section? If you can help me, so much appreciated.

As has been said in comments, the
outerHTMLis a serialization of the DOM representation after parsing of the original markup. This may be completely different than the original markup and will most likely not match in size at all:To get the size of the original markup you can call the
performance.getEntriesByType("navigation")method, which will return an array ofPerformanceNavigationTimingobjects, where you'll find the one of the initial document (generally at the first position). ThesePerformanceNavigationTimingobjects have fields that let you know the decoded-size of the resource, its coded-size (when compressed over the wire), and its transferred-size (can vary if cached).Unfortunately, this API doesn't work well for iframes (moreover the ones that are fetched through POST requests like StackSnippets), so I have to outsource the live demo to this Glitch project.
The main source is: