Gino Cochuyt spent some hours to find the cause of memory leaks in IE when using instanceof.

He found at least 3 Objects that cause the memory leaks:

  • window
  • document
  • element

Just using - if (window instanceof Object) - for example causes a memory leak! that grows by 50 KB per refresh; the reason for this is because these 3 Objects are not really objects in IE (they don’t have a constructor); the same reason why they don’t have the Native Object methods:

  • hasOwnProperty
  • valueOf

where in other Browsers like FF, Opera and Safari … (the ones that i tested!)
a div elemement for example is an instanceof HTMLDivElement - HTMLElement - Node etc..

To prevent these memory leaks:

JAVASCRIPT:

  1.  
  2. function MyClass(){};
  3.  
  4. function testIt (obj) {
  5.         if (!obj || !obj.hasOwnProperty || !(obj instanceof MyClass)) {}
  6.         // IE will fail on the second statement if obj == window || document…. so the instanceof statement doesn’t get tested!!
  7. };
  8.