开发者

javascript: how do we track the total number of objects (any object will be counted) in the page

I want to track the total number of objects created in the page (i'm testing/analysing something).

Is it possible to do so? (like if i press a button it will alert 1300 if 1300 objects are created)

Btw I'm not checking how many objects currently exists, im tracking the total number of objects "ever created".

I was thinking of modifying the Object.prototype.constructor and add some tracking mechanism there but its not a writable property

Edit: I'm trying to find out if i run this code:

var Test=function(){
  return {};
};
//start tracker
new Test();
//end tracker

how many objects are created between // start tracker and //end tracker (i'm sus开发者_开发问答pecting 2 objects, but i just want to be sure)


When a function f is called as a constructor (new f()), a new object is created and provided as the this value for the call. Read about the [[Construct]] internal method here.

Therefore, new Test() will create (at least) 2 objects:

  • an object that is created automatically (and bound to this)
  • an object that is created by your object literal expression ({})


The Chrome developer tools include a "heap profiler" which can tell you how many objects of each type currently exist, and how much memory they're using.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜