diff --git a/docs/v8-tips/freeing-memory.md b/docs/v8-tips/freeing-memory.md index 08caa7f..cc0a114 100644 --- a/docs/v8-tips/freeing-memory.md +++ b/docs/v8-tips/freeing-memory.md @@ -1,18 +1,20 @@ # Freeing memory -If you want to delete a property of an `Object`, you can set the property to `null`: +If you want to remove a property from an `Object`, probably you are considering use `delete`: ```js -// Inefficient way (bad) var foo = { bar: 'hello world' } -delete foo.bar +delete foo.bar // Inefficient way (bad) +``` + +But this action have serious performance degradation. A most common technique with the same effect is assign `undefined`: -// Efficient way (good) +```js var foo = { bar: 'hello world' } -foo.bar = null +foo.bar = undefined // Efficient way (good) ``` -If you want do delete the `Object` entirely, then simply write `foo = null`. +The problem with `delete` boils down to the way V8 handles the dynamic nature of JavaScript objects and the (also potentially dynamic) prototype chains that make property lookups even more complex at an implementation level. The garbage collector is interested in `Object`s that are not referenced by any other `Object`. On the other hand, JavaScript engines can detect such *hot* `Object`s and attempt to optimize them.