Pacerier
Pacerier

Reputation: 89623

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 suspecting 2 objects, but i just want to be sure)

Upvotes: 0

Views: 358

Answers (2)

Šime Vidas
Šime Vidas

Reputation: 185933

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 ({})

Upvotes: 1

Alnitak
Alnitak

Reputation: 339816

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.

Upvotes: 1

Related Questions