How to get the size of a JavaScript object?

I want to know the size occupied by a JavaScript object.

Take the following function:

function Marks(){
  this.maxMarks = 100;
}

function Student(){
  this.firstName = "firstName";
  this.lastName = "lastName";
  this.marks = new Marks();
}

Now I instantiate the student:

var stud = new Student();

so that I can do stuff like

stud.firstName = "new Firstname";

alert(stud.firstName);

stud.marks.maxMarks = 200;

etc.

Now, the stud object will occupy some size in memory. It has some data and more objects.

How do I find out how much memory the stud object occupies? Something like a sizeof() in JavaScript? It would be really awesome if I could find it out in a single function call like sizeof(stud).

I’ve been searching the Internet for months—couldn’t find it (asked in a couple of forums—no replies).

22 Answers
22

Leave a Comment