Does Python GC deal with reference-cycles like this?

Python’s standard reference counting mechanism cannot free cycles, so the structure in your example would leak. The supplemental garbage collection facility, however, is enabled by default and should be able to free that structure, if none of its components are reachable from the outside anymore and they do not have __del__() methods. If they do, … Read more

Why does sys.getrefcount() return 2?

When you call getrefcount(), the reference is copied by value into the function’s argument, temporarily bumping up the object’s reference count. This is where the second reference comes from. This is explained in the documentation: The count returned is generally one higher than you might expect, because it includes the (temporary) reference as an argument … Read more

Python del statement

The del statement doesn’t reclaim memory. It removes a reference, which decrements the reference count on the value. If the count is zero, the memory can be reclaimed. CPython will reclaim the memory immediately, there’s no need to wait for the garbage collector to run. In fact, the garbage collector is only needed for reclaiming … Read more

What’s the best way (most efficient) to turn all the keys of an object to lower case?

The fastest I come up with is if you create a new object: var key, keys = Object.keys(obj); var n = keys.length; var newobj={} while (n–) { key = keys[n]; newobj[key.toLowerCase()] = obj[key]; } I’m not familiar enough with the current inner working of v8 to give you a definitive answer. A few years ago … Read more