Skip to content

Commit

Permalink
Add Array reusing instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Mar 30, 2016
1 parent 165b141 commit edffef8
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
+ [Error](#error)
- [Avoid try/catch](#avoid-try-catch)
+ [Function](#function)
- [Make Your Constructors new-Agnostic](#make-your-constructors-new-agnostic)
- [Avoid .bind, is slower.](#avoid-bind--is-slower)
+ [Array](#array)
- [Reusing instances](#reusing-instances)
- [Array.pop() better than Array.shift()](#arraypop---better-than-arrayshift--)
- [License](#license)

Expand Down Expand Up @@ -506,6 +508,25 @@ sayMonday('Kiko')

### Array

#### Reusing instances

A good approach for performance is reuse instance in favour to avoid create a new instance and the costs that it represents.

For do it, use `.length` to eliminate the content and reuse it safely:

```js
var arr = [1, 2, 3, 4, 5]

/* do something */

arr = [] // bad
arr.length = 0 // good!
```

It deletes everything in the array, which does hit other references.

For `Object` or `Function` I recommend use a pool of instances like [reusify](https://github.com/mcollina/reusify#reusify).

#### Array.pop() better than Array.shift()

The `.shift` method removes the first element from an array and returns it.
Expand All @@ -516,6 +537,7 @@ On the other hand, `.pop` can simply subtract 1 from its length.

Then `.shift` is usually much slower than `.pop`.


# License

MIT © [Kiko Beats](http://kikobeats.com)

0 comments on commit edffef8

Please sign in to comment.