简体   繁体   中英

Disable infinite loop protection in codepen.io

Is it possible to disable infinite loop protection in codepen.io?

JSBin has // noprotect .

It would be fine to be able to run the code without any additions made by editor , eg. when measuring execution times etc.

When I run CPU profiler, there is a call to window.CP.PenTimer.shouldStopLoop(), maybe this causes errors in measuring execution times.

In JSBin exactly the same code runs 1000 ms, but in codepen.io the execution time is 4000 ms. This makes codepen.io less suitable to run heavy loops.

Example:

// noprotect
var len = 2000000;
var a = [];
var t = performance.now();
while(len--)
{
  a.push((len%7)*Math.atan2(len,-len));
}
t = performance.now() - t;
document.write(t);

In Codepen: http://codepen.io/timo22345/pen/yYedmo 4202.92 ms (Chrome 45 OSX)

In JSBin: http://jsbin.com/xufesa/2/edit?html,css,js,output 1082.03 ms (Chrome 45 OSX)

This like huge slowdown makes codepen.io a nice text editor (nice in a way that it is nearer to desktop text editors than JSBIN, because eg. tab key works as expected and it is possible to search within iframes), but after the code is in final state, you have to move it to JSBin or other server to run the code - if non-throttled speed is something that matters.

At least some warning would be fine to avoid completely wrong performance measurements and wrong conclusions. See eg. http://www.sitepoint.com/measuring-javascript-functions-performance/ in which the writer measures performance of various functions using codepen.io and after measuring the execution times finds that a function using forEach runs significantly faster than function using plain for loop. And his conclusion is:

The explanation is simple but subtle. The first function which uses haystack.forEach benefits from some low-level optimizations in the browser's JavaScript engine that we don't get when we use an array index technique. It proves our point: you never know until you measure it!

Vyacheslav Egorov comments that this is not true:

A more obvious explanation is that something went wrong with an experiment... And indeed it did: if you put a debugger statement into for loop you will discover that CodePen instruments loops with preemption checks (to prevent infinite loops from killing UI, I guess). These checks look like this:

if (window.CP.shouldStopExecution(1)) {
  break;
}

You can use:

window.CP.PenTimer.MAX_TIME_IN_LOOP_WO_EXIT = Number.POSITIVE_INFINITY;

reference: Codepen blog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM