Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
950 views
in Technique[技术] by (71.8m points)

performance - JavaScript Code Works Faster when JS Profiling is on? What?

I'm facing a very strange behaviour with my application - if I start the profiler (must be JS profiler), the code works almost twice as fast.

I've reproduced it with a very simple code which can be found on this fiddle: https://jsfiddle.net/zagrwk44/

The thing is that this reproduces only on machines with old graphic cards. I've managed to reproduce it on a machine with AMD Radeon HD 6450 graphic card. On a newer machine this does not reproduce anymore.

How come the profiler makes the code run faster?? almost twice as fast!

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

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

I'm starting and stopping the profiler via javascript with console.profile and console.profileEnd. In order to reproduce it, DevTools must be opened when running.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Can't repro on my machine.
But I'm curious do you compare a run with DevTools open vs a run with profiler? If so, the explanation could be that DevTools disables internal notifications, like updates to the Elements panel when profiling is active.
If you're comparing a run with DevTools closed, then it really looks strange.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...