JavaScript - Profiling

javascript

SunSpider JavaScript Benchmark
JSLitmus
http://mootools.net/slickspeed/
http://benchmarkjs.com/
http://jsperf.com/

<script type="text/javascript">
    profileInfo = {}
    function startProfileTimer(reason) {
        if (typeof(profileInfo[reason]) == "undefined") {
            profileInfo[reason] = {};
            profileInfo[reason].counter = 0;
            profileInfo[reason].totalTime = 0;
        }
        profileInfo[reason].startTime = new Date();
        profileInfo[reason].counter = profileInfo[reason].counter + 1;
    }
    function endProfileTimer(reason) {
        var endTime = new Date();
        var startTime = profileInfo[reason].startTime;
        var diff = endTime.valueOf() - startTime.valueOf();
        profileInfo[reason].totalTime = profileInfo[reason].totalTime + diff;
    }
</script>

The startProfileTimer and endProfileTimer function takes one parameter which is a string. In order for this to work, the string that you specify for startProfileTimer must exactly equal with the string you specify for endProfileTimer. This pair of functions capture the number of times a particular code block is executed, and the total amount that it take to execute this code block. At the end, you must inspect the profileInfo variable and compute the averages.

Microsoft Developer Tool and Firebug both have ability to profile your code. Give that a try too.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License