With a JavaScript Array, I can reset it to an empty state with a single assignment:
array.length = 0;
This makes the Array "appear" empty and ready to reuse, and as far as I understand is a single "operation" - that is, constant time.
Is there a similar way to clear a JS Object? I know I can iterate its fields deleting them:
for (var prop in obj) { if (obj.hasOwnProperty(prop)) { delete obj[prop]; } }
but this has linear complexity.
I can also just throw the object away and create a new one:
obj = {};
But "promiscuous" creation of new objects leads to problems with Garbage Collection on IE6. (As described here)
Well, at the risk of making things too easy...
for (var member in myObject) delete myObject[member];
...would seem to be pretty effective in cleaning the object in one line of code with a minimum of scary brackets. All members will be truly deleted instead of left as garbage.
Obviously if you want to delete the object itself, you'll still have to do a separate delete() for that.