Count functions calls with JavaScript - Stack Overflow

For example: I have a lot of functions and use them many times.I need to count calls for each function

For example: I have a lot of functions and use them many times. I need to count calls for each function. What is the best practice to make it?

At first i thought i need closures, but I can't implement it in a right way.

For example: I have a lot of functions and use them many times. I need to count calls for each function. What is the best practice to make it?

At first i thought i need closures, but I can't implement it in a right way.

Share Improve this question asked Jul 31, 2015 at 7:40 Nikolay AleshkovskiyNikolay Aleshkovskiy 7272 gold badges8 silver badges20 bronze badges 3
  • on what end do you need the result? back/user? – ted Commented Jul 31, 2015 at 7:42
  • Are the results a continuous count, session based, etc?? – 1'' Commented Jul 31, 2015 at 7:45
  • i need just vars with number of calls, it doesn't matter – Nikolay Aleshkovskiy Commented Jul 31, 2015 at 20:49
Add a ment  | 

5 Answers 5

Reset to default 4

In the simplest case, you can decorate each function with a profiling wrapper:

_calls = {}

profile = function(fn) {
    return function() {
        _calls[fn.name] = (_calls[fn.name] || 0) + 1;
        return fn.apply(this, arguments);
    }
}

function foo() {
    bar()
    bar()
}

function bar() {
}

foo = profile(foo)
bar = profile(bar)

foo()
foo()

document.write("<pre>" + JSON.stringify(_calls,0,3));

For serious debugging, you might be better off with a dedicated profiler (usually located in your browser's console).

You could try something like this:

<script>
    var aCalls = 0;
    var bCalls = 0;
    function a()
    {
        aCalls = aCalls + 1;
        alert(aCalls);
    }
    function b()
    {
        bCalls = bCalls + 1;
        alert(bCalls);
    }
</script>
var count = 0;

function myfunction()
{
    count++;
    alert( "Function called " + count);
}


myfunction();
myfunction();

http://jsfiddle/xsdzpmwm/3/

The best way is to use a profiler.

On IE: press F12 to open developer tools, then go to the Profiler tab, and hit the play button. After stopping the profiler, you'll be presented with a lot of info (number of calls for each function, inclusive time, exclusive time, etc.)

On Chrome: press F12, go to Profiles, Collect JavaScript CPU Profile (that won't tell you the number of calls though)

My approach would add a property “count” to the function itself.

Just add one line at the beginning of your function you want to have tracked calls.

function func() {
  func.count = (func.count || 0) + 1;
  // first time you call the function func.count is undefined so use 0 instead 
  console.log("hi");
}

func();
console.log(func.count) // 1
func();
func();
func();
console.log(func.count) // 4

Functions are objects in javascript after all. No pollution of global namespace, no wrapping or closures needed, very simple to understand and to write.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744104853a4558679.html

相关推荐

  • Count functions calls with JavaScript - Stack Overflow

    For example: I have a lot of functions and use them many times.I need to count calls for each function

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信