Number of Javascript lines executed during page load

最后都变了- 提交于 2019-12-06 21:55:06

问题


I have an use case where I need to get the total number of Javascript lines executed when my web page is loading.

The problem I am facing is that the browser throws an alert when a certain number of JS execution exceed ( 5 million I think in case of IE ) and the page gets hanged.

I used the profiler available in IE Developers tool bar,but it gives me the total number of JS functions called but not the total number/count of lines executed.

Any help is appreciated.

Thanks


回答1:


This may help:

http://www-archive.mozilla.org/performance/jsprofiler.html

The first line specifies the js file from which times were gathered. Each line below that is of the format: [A, B] C {D-E} F {G, H, I}

D -> Base line number of the function in the JS file
E -> Extent (last) line number of the function in the JS file




回答2:


First of all, you should really redesign your code; 5 million statements without handing control back to the browser is by all means a lot of code, imagine how mobile browsers will struggle with it.

One way to find out how many statements are executed is by instrumenting your code, adding one statement for each statement in your code to count the number of times it was executed, effectively doubling up the number of statements in your code.

There are also code coverage tools that can run inside the browser without having to instrument your code at all; Googling for "javascript code coverage" gives a fair amount of browser extensions that you could use, such as hrtimer.




回答3:


Just split it into smaller chunks and call after a timeout - that gets around the IE issue. e.g.

somethingBig();
somethingElseBig();

instead write :

somethingBig();
setTimeout(somethingElseBig);



回答4:


In BASH:

wc -l file.js

In PHP:

<?php
$c=0;
$file_handle = fopen("file.js", "r");
while (!feof($file_handle)) {
    $line = fgets($file_handle);
    $c++;
}
fclose($file_handle);
print "$c lines.\n";
?>

In Node.js:

var i, count = 0, js=require('fs').createReadStream('file.js');

js.on('data', function(chunk) {
    for (i=0; i < chunk.length; ++i)
        if (chunk[i] == 10) count++;
});

js.on('end', function() {
    console.log(count);
});

Try minification to get your line-numbers down.

This seems like a terrible-lot of code, though.



来源:https://stackoverflow.com/questions/13364177/number-of-javascript-lines-executed-during-page-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!