Skip to content

Commit

Permalink
Update freeing-memory.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Sep 22, 2017
1 parent 476e850 commit 3a9a610
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions docs/v8-tips/freeing-memory.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down

0 comments on commit 3a9a610

Please sign in to comment.