Ways4eu WordPress.com Blog

SPA View of ways4eu.wordpress.com

[AskJS] Using console.time to calculate the time it takes a block of code to execute

By ways4.eu

on Sat Apr 25 2020

Hi All,

I have recently become interested in the web performance side of web development. I’m wondering if the way I’m calculating the amount of time it takes a block of code to run correct and trustworthy?

Essentially what I do is wrap a console.time(), and a console.timeEnd around a block of code like so.

function copyToClipboard1(e) { console.time('Exec #1'); const clipboard = emailInput; clipboard.value = this.value; clipboard.select(); document.execCommand("copy"); console.timeEnd('Exec #1'); } 

This then returns something like this Navigator #1:0.0869140625ms. Is this an efficient way of calculating the speed of a code block. The result each time are usually similar enough?

submitted by /u/Deviso
[link][comments]

from πš“πšŠπšŸπšŠπšœπšŒπš›πš’πš™πš https://ift.tt/2S6lddz