Is JavaScript code faster when JS Profiling is enabled? What kind?

I encounter a very strange behavior with my application - if I run the profiler (there must be a JS profiler), the code runs almost twice as fast.

I reproduced it using a very simple code, which can be found on this script: https://jsfiddle.net/zagrwk44/

The fact is that this is reproduced only on machines with old graphics cards. I was able to play it on a machine with an AMD Radeon HD 6450 graphics card. On a newer machine, this no longer plays.

Why does a profiler make code faster? almost twice as fast !

The code that takes time here is simply changing the position of the div on the screen:

for (var i = 0; i < 1000000; i++) { box.style.top = getRandomInt(0, 100) + '%'; box.style.left = getRandomInt(0, 100) + '%'; }; 

I start and stop the profiler through javascript using console.profile and console.profileEnd . To play it, DevTools must be opened at startup.

Thanks!

+5
source share
2 answers

Unable to play on my machine.
But I'm curious if you compare running with DevTools open versus running with a profiler? If so, the explanation may be that DevTools disables internal notifications, such as updates in the Elements panel when profiling is active.
If you are comparing launch with DevTools, then this looks weird.

+2
source

I found this strange behavior that only happens to me on Windows Server 2008 R2.

your tester is not a correct indication, as it checks the performance with a random function that can lead to different results for each sample, moreover, you do not exclude console.profile () and console.profileEnd () from the temporary selection, which means that you have never had real initial results.

To get a better and more realistic result, the code should look like this:

 var random = [80,90,15,5,70,50,60,25,36,45,62,58,76,23,93]; fbtn.addEventListener('click', function() { //START PROFILE BEFOR TIME START if (withProfiling.checked) { console.profile(); } console.time('click handler'); for (var i = 0, v =0; i < 1000000; i++, v++) { box.style.top = random[v] + '%'; //USE SAME NUMBERS FOR ALL TESTS box.style.left = random[v] + '%';//USE SAME NUMBERS FOR ALL TESTS if(v >= 14){ v= 0; } }; console.timeEnd('click handler'); // STOP PROFILE AFTER TIME END if (withProfiling.checked) { console.profileEnd(); } }); 

here is a tester that could trace it in more detail: http://embed.plnkr.co/bdreL4UVFyyWtDoNTXRs/

I delete the original profiler by code because I found that this strange behavior is restored better by manually running the profiles.

to restore it, select β€œGet Javascript CPU Profile” from DevTools profiles in Chrome.

Hope this was helpful

Menachem

0
source

Source: https://habr.com/ru/post/1239020/


All Articles