Skip to content

Commit

Permalink
Refactor sections
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Aug 27, 2016
1 parent 4a7645b commit e22098b
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 55 deletions.
6 changes: 3 additions & 3 deletions array/arguments.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#### All you need to know about `arguments`
# `arguments` is special

There are numerous ways to use arguments in a way that causes the function to be unoptimizable. One must be extremely careful when using arguments.
There are numerous ways to use `arguments` in a way that causes the function to be unoptimizable. One must be extremely careful when using `arguments`.

Only use:

* `arguments.length`.
* `arguments[i]` where `i` is always a valid integer index into the `arguments`, and can not be out of bound.
* Never use `arguments` directly without `.length` or `[i]`.
* STRICTLY `fn.apply(y, arguments)` is ok, nothing else is, e.g. `.slice`. `Function#apply` is special.
* STRICTLY `fn.apply(y, arguments)` is ok, nothing else is, e.g. `.slice`, That's because `Function#apply` is special.
* Be aware that adding properties to functions (e.g. `fn.$inject =...`) and bound functions (i.e. the result of `Function#bind`) generate hidden classes and, therefore, are not safe when using `.apply`.
2 changes: 1 addition & 1 deletion array/pop-or-shift.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#### Array.pop() better than Array.shift()
# pop over shift

The `.shift` method removes the first element from an array and returns it.

Expand Down
17 changes: 13 additions & 4 deletions array/reallocation.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
# Reallocation

A good approach for performance is reuse instance in favour to avoid create a new instance and the costs that it represents.
Instead of declare a new `Array` and reserving memory for that, you can reallocate an `Array` previously declared.

If you want to remove the content of an array, use [`Array.prototype.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length):
The thing that you need is remove the content of the array; You can do that using [`Array.prototype.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length):

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

/* do something */

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

Another good approach to use `.length` is when you know the size of your `Array`. In this case you can reserve the necessary memory space:

```js
var array = []
arr.length = 5

arr[0] = 1
arr[1] = 2
arr[2] = 3
```
9 changes: 7 additions & 2 deletions date/timestamp.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#### Create timestamps with `Date.now()`
# Creating timestamps

Normally for create timestamps you have two ways:

- `Date.now()`
- `new.Date().getTime()`

Althought `Date.now()` and `new.Date()` have the same behavior, `Date.now` is faster because you are not allocating and object and then calling the method of the object.

This is specially remarkable when you make sucesive calls (for example, when you append timestamp in logs).
This is specially remarkable when you make sucesive calls (for example, when you append timestamps in logs).

Another thing to be considered is the fact that, when you create a `new Date` you are linking a moment of the time with the object. Sucesive calls to `.getTime` have the same output.

Expand Down
24 changes: 14 additions & 10 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
# Table of contents

* [JS MythBusters](/README.md)
* [Send your tip!]()
* [Bibliography](/bibliography.md)
* [Resources](/resources.md)

* [Array]
* [Reallocation](/array/reallocation.md)
* [Array.pop() better than Array.shift()]
* [All you need to know about arguments]
* [.pop over .shift](/array/pop-or-shift.md)
* [arguments is special](/array/arguments.md)
* [Date]
* [Create timestamps with Date.now()]
* [Creating timestamps](/date/timestamp.md)
* [Error]
* [Avoid try/catch]
* [Avoid try/catch](/error/try-catch.md)
* [Function]
* [Make Your Constructors new-Agnostic]
* [Avoid .bind, is slower]
* [new agnostic](/function/new.md)
* [.bind is slower](/function/bind.md)
* [RegexEp]
* [Use RegExp in cases with sense]
* [Focus RegExp on failing faster]
* [Use the correct RegExp method]
* [Common sense](/regexp/common-sense.md)
* [Focus on failing faster](/regexp/fail-faster.md)
* [Correct method](/regexp/methods.md)
* [String]
* [Instead of String.concat, use '+=']
* [+= for concat](/string/concat.md)
2 changes: 1 addition & 1 deletion docs/assets/css/build.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions docs/assets/scss/_style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ html .markdown-body pre code {
background: transparent;
}

html .markdown-body p > code,
html .markdown-body li > code {
color: $code-inline-color;
}

.markdown-body .pl-c {
color: $gray-darkest;
}
Expand Down Expand Up @@ -110,6 +115,10 @@ html .markdown-body h4 + p {
margin-top: -.9em;
}

html .markdown-body li+li {
margin-top: .25em;
}

/*
* Update heading styles
*/
Expand Down Expand Up @@ -231,6 +240,7 @@ html .toc-menu .hlink {
html .toc-menu .link.-active,
html .toc-menu .hlink.-active {
box-shadow: inset -2px 0 $secondary-color;
color: $white !important;
}

/*
Expand Down
7 changes: 4 additions & 3 deletions docs/assets/scss/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ $gray-lightest : #f8f8f8;
$primary-font : Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,serif;
$secondary-font : $primary-font;

$primary-color : #2EFFFE;
$secondary-color : #0CEDEC;
$link-color : #14B3B2;
$primary-color : lighten(#00bcd4, 5%);
$secondary-color : #00bcd4;
$link-color : darken(#00bcd4, 5%);

$primary-text-color : $gray-dark;
$secondary-text-color : $gray-darkest;
$code-color : $gray;
$code-inline-color : $link-color;


$background-color: $black;
2 changes: 1 addition & 1 deletion docs/docpress.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"github": "rstacruz/onmount",
"github": "Kikobeats/js-mythbusters",
"css": [
"assets/css/build.css"
],
Expand Down
12 changes: 8 additions & 4 deletions error/try-catch.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
#### Avoid try/catch
# Avoid try/catch

Certain constructs like `try/catch` are considered not optimizable for the JavaScript engine, so avoid handle business logic inside. Just for pass an error as callback and this type of things.
Certain constructs like `try/catch` are considered not optimizable for the JavaScript engine, so avoid handle business logic inside.

Just for pass an error as callback and this type of things.

Or maybe the think that you need to avoid is synchronous code? :P

A good approach for support optimization with throweable code is return an `Error` an later check about the type.

```js
function mayBeError () {
function maybeError () {
/* ... */
}

var result = maybeError()

if (result instanceof Error) {
/* do something under error */
/* do something under error */
}

/* in other case no error */
Expand Down
25 changes: 21 additions & 4 deletions function/bind.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#### Avoid .bind, is slower.
# `.bind` is slower

In general terms, `.bind` the context with a `Function` generate need a considerable effort.

Expand All @@ -7,11 +7,11 @@ Native method is, in general JavaScript Engines slow. Instead, you can use:
- `.call` method (when you need to call the function once).
- `var self = this` is simple and effective.

> Otherwise, exists a set of alternatives, such as libraries that try to implement a better way to do the same. In special, Lodash implementation is based in bitwise, that is very fast check in JavaScript. Check the [test](https://jsperf.com/bind-vs-jquery-proxy/104) benchmark.
> Otherwise, exists a set of alternatives, such as libraries that try to implement a better way to do the same. In special, [Lodash](https://lodash.com) implementation is based in bitwise, that is very fast check in JavaScript. Check the [test](https://jsperf.com/bind-vs-jquery-proxy/104) benchmark.
I usually use `.bind` more oriented to bind arguments that doesn't change around Function:
If you are using `.bind` oriented to memoize arguments values that doesn't change around the `Function` calls, like:

```
```js
function sayHello(day, name) {
console.log('Hello ' + name + ', have a good ' + day)
}
Expand All @@ -22,3 +22,20 @@ var sayMonday = sayHello.bind(null, day)
sayMonday('Kiko')
// => 'Hello Kiko, have a good monday'
```

The real thing that you need is a *partial function*. The implementation depends of the library that you use but normally it's more faster than `.bind`:

```js
function greet(greeting, name) {
return greeting + ' ' + name;
}

var sayHelloTo = _.partial(greet, 'hello');
sayHelloTo('fred');
// ➜ 'hello fred'

// Partially applied with placeholders.
var greetFred = _.partial(greet, _, 'fred');
greetFred('hi');
// ➜ 'hi fred'
```
4 changes: 2 additions & 2 deletions function/new-agnostic.md → function/new.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#### Make Your Constructors new-Agnostic
# `new` agnostic

Even your function is called or not using `new` keyword, you can force to have the same behavior in both cases:

Expand All @@ -13,7 +13,7 @@ function User (name, passwordHash) {

Now, the behavior is the expected in both cases:

```
```js
var x = User("baravelli", "d8b74df393528d51cd19980ae0aa028e")
var y = new User("baravelli","d8b74df393528d51cd19980ae0aa028e")

Expand Down
10 changes: 6 additions & 4 deletions regexp/common-sense.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#### Use RegExp in cases with sense.
# Use RegExp in cases with sense

When use with care, regexes are veru fast. However, They're suauly overkill when you are merely searching for literal strings:
When use with care, `RegexEp` are fast.

However, They're usually overkill when you are merely searching for literal strings:

```js
var endsWithSemiColon = /;$/.test(str)
```

Each time a semicolon is found, the regex advances to the next token (`$`), which checks wheter the match is at the end of the string. If not, the regex continues searching for a match until it finally makes its way throough the entire string.
Each time a semicolon is found, the `RegExp` advances to the next token (`$`), which checks wheter the match is at the end of the string. If not, the `RegExp` continues searching for a match until it finally makes its way throough the entire string.

In this case, a better approach is to skip all the intermediate steps required by a regex and simply check whether the last character is a semicolon:
In this case, a better approach is to skip all the intermediate steps required by a `RegExp` and simply check whether the last character is a semicolon:

```js
var endsWithSemiColon = str.charAt(str.length - 1) === ';'
Expand Down
Loading

0 comments on commit e22098b

Please sign in to comment.