'Is it possible to use a high resolution timer from within an iframe for benchmarking absolute performance against the parent document?

I am trying to benchmark the performance implications of various iframe setups.

I would like to know how long it takes from the start of the root document until a certain point within an iframe.

One option is to use Date.now() and compare that to the host's performance.timeOrigin of the root page like so:

<body>
   <iframe> <!-- cross origin iframe -->
     <script>console.log(Date.now())</script>
   </iframe>
   <script>console.log(performance.timeOrigin)</script>
</body>

The issue is that Date.now() offers inaccurate times and I would rather use a high resolution timer offered by the performance API.

Trying to use performance.now() gives me a relative duration since the start of the current frame, however I am trying to obtain the relative duration since the start of the rootmost document.

So if you imagine something like this:

<body>
   <iframe>
     <script>console.log(performance.now())</script> // 8ms
   </iframe>
   <script>console.log(performance.now())</script> // 30ms
</body>

There does not appear to be a way to get a high resolution absolute time using the performance API. Any ideas?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source