sathishvisa
sathishvisa

Reputation: 315

Number of Javascript lines executed during page load

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

Upvotes: 9

Views: 1220

Answers (4)

user1906354
user1906354

Reputation:

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);

Upvotes: 1

Ja͢ck
Ja͢ck

Reputation: 173642

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.

Upvotes: 1

nullable
nullable

Reputation: 2581

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

Upvotes: 1

konsumer
konsumer

Reputation: 3503

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.

Upvotes: 0

Related Questions