Skip to content

Commit

Permalink
Add v8 tips
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Aug 27, 2016
1 parent e22098b commit b7dbbb3
Show file tree
Hide file tree
Showing 18 changed files with 72 additions and 43 deletions.
23 changes: 22 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
# Table of contents

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

* [Workflow]
* [Boolean condition](/workflow/boolean-conditions.md)
* [Lookup table](/workflow/lookup-table.md)
* [Math methods](/workflow/math.md)
* [Memoizaton](/workflow/memoization.md)
* [Scope](/workflow/scope.md)
* [Variable access](/workflow/variable-access.md)

* [V8]
* [Hidden Class](/v8-tips/hidden-class.md)
* [Float Number](/v8-tips/float-number.md)
* [Inline initialization](/v8-tips/inline-initialization.md)
* [Monomorphic](/v8-tips/monomorphic.md)
* [use strict](/v8-tips/use-strict.md)
* [Freeing memory](/v8-tips/freeing-memory.md)

* [Array]
* [Reallocation](/array/reallocation.md)
* [.pop over .shift](/array/pop-or-shift.md)
* [arguments is special](/array/arguments.md)

* [Date]
* [Creating timestamps](/date/timestamp.md)

* [Error]
* [Avoid try/catch](/error/try-catch.md)

* [Function]
* [new agnostic](/function/new.md)
* [.bind is slower](/function/bind.md)

* [RegexEp]
* [Common sense](/regexp/common-sense.md)
* [Focus on failing faster](/regexp/fail-faster.md)
* [Correct method](/regexp/methods.md)

* [String]
* [+= 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.

11 changes: 4 additions & 7 deletions docs/assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ function ready (fn) {
}
}

function addClass (el, className) {
if (el.classList) el.classList.add(className)
else el.className += ' ' + className
}

ready(function () {
addClass(document.body, 'hack')
addClass(document.body, 'dark')
var links = document.querySelectorAll('.markdown-body a')
links.forEach(function (link) {
link.setAttribute('target', '_blank')
})
})
8 changes: 8 additions & 0 deletions docs/assets/scss/_style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ html .markdown-body li > code {
color: $gray-darkest;
}

.markdown-body .pl-pds, .markdown-body .pl-s, .markdown-body .pl-s .pl-pse .pl-s1, .markdown-body .pl-sr, .markdown-body .pl-sr .pl-cce, .markdown-body .pl-sr .pl-sra, .markdown-body .pl-sr .pl-sre {
color: $golden;
}

/*
* Links and fonts
*/
Expand Down Expand Up @@ -387,3 +391,7 @@ table.no-head thead {
.font-large {
font-size: x-large;
}

a.link.title.link-send-tip {
color: $special-text-color !important;
}
3 changes: 3 additions & 0 deletions docs/assets/scss/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

$white : #fafbfb;
$black : black;
$golden : #e5b567;
$pink : #ff2e88;

$gray-darkest : #333;
$gray-darker : #666;
Expand All @@ -20,6 +22,7 @@ $link-color : darken(#00bcd4, 5%);

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

Expand Down
Empty file added send-tip.md
Empty file.
6 changes: 3 additions & 3 deletions v8-tips/float-number.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
### Whole than Float Number
# Float Number are expensive

V8 uses tagging to represent values efficiently when types can change.

V8 infers from the values that you use what number type you are dealing with. Once V8 has made this inference, it uses tagging to represent values efficiently, because these types can change dynamically. However, there is sometimes a cost to changing these type tags, so it's best to use number types consistently, and in general it is most optimal to use 31-bit signed integers where appropriate.
V8 infers from the values that you use what number type you are dealing with. Once V8 has made this inference, it uses tagging to represent values efficiently, because these types can change dynamically. However, there is sometimes a cost to changing these type tags, so it's best to use number types consistently, and in general it is most optimal to use **31-bit signed integers** where appropriate.

```
```js
var i = 42; // this is a 31-bit signed integer (whole).
var j = 4.2; // this is a double-precision floating point number (float).
```
Expand Down
10 changes: 5 additions & 5 deletions v8-tips/freeing-memory.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
### Freeing memory by assigning 'null'.
# Freeing memory by assigning `null`

If you want to delete a property of a object uses
If you want to delete a property of a object uses:

```
```js
var foo = { bar: 'hello world' };
foo.bar = null;
```

Instead of:

```
```js
var foo = { bar: 'hello world' };
delete foo.bar;
```
Expand All @@ -22,4 +22,4 @@ On the other hand, JavaScript engines can detect such "hot" objects and attempt

This also is applicable to `Arrays`

[Writing Fast, Memory-Efficient JavaScript @ smashingmagazine](http://www.smashingmagazine.com/2012/11/writing-fast-memory-efficient-javascript/#de-referencing-misconceptions).
[Writing Fast, Memory-Efficient JavaScript @ smashingmagazine](http://www.smashingmagazine.com/2012/11/writing-fast-memory-efficient-javascript/#de-referencing-misconceptions).
2 changes: 1 addition & 1 deletion v8-tips/hidden-class.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### Hidden Class in Constructors
# Hidden Class in Constructors

JavaScript has limited compile-time type information: types can be changed at runtime, so it's natural to expect that it is expensive to reason about JS types at compile time.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### Object/Arrays initialization inline
# Inline Initialization

```js
var array = []
Expand All @@ -8,13 +8,11 @@ array[2] = 0.5 // Allocates, converts
array[3] = true // Allocates, converts
```

is less efficient than:

```js
var array = [77, 88, 0.5, true]
```

Because in the first example the individual assignments are performed one after another, and the assignment of `a[2]` causes the Array to be converted to an Array of unboxed doubles, but then the assignment of `a[3]` causes it to be re-converted back to an Array that can contain any values (`Number` or `Object`). In the second case, the compiler knows the types of all of the elements in the literal, and the hidden class can be determined up front.
Because in the first example the individual assignments are performed one after another, and the assignment of `a[2]` causes the `Array` to be converted to an `Array` of unboxed doubles, but then the assignment of `a[3]` causes it to be re-converted back to an `Array` that can contain any values (`Number` or `Object`). In the second case, the compiler knows the types of all of the elements in the literal, and the hidden class can be determined up front.

Another example:

Expand Down
10 changes: 6 additions & 4 deletions v8-tips/monomorphic.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
### Monomorphic over Polymorphic types
# Monomorphic over Polymorphic types

Operations are monomorphic if the hidden classes of inputs are always the same - otherwise they are polymorphic, meaning some of the arguments can change type across different calls to the operation. For example, the second add() call in this example causes polymorphism:
Operations are **monomorphic** if the **hidden classes** of inputs are always the same - otherwise they are **polymorphic**, meaning some of the arguments can change type across different calls to the operation.

```
For example, the second `add()` call in this example causes polymorphism:

```js
function add(x, y) {
return x + y;
}
Expand All @@ -11,4 +13,4 @@ add(1, 2); // + in add is monomorphic
add("a", "b"); // + in add becomes polymorphic
```

Monomorphic types is more predictable the Compiler and more easy to generate good code.
**Monomorphic** types is more predictable for the compiler and more easy to generate good code.
2 changes: 1 addition & 1 deletion v8-tips/use-strict.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### More safe (and fast) code with 'use strict'.
# Faster (and safe) using `use strict`

From [MDN use strict](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode) section:

Expand Down
6 changes: 3 additions & 3 deletions workflow/boolean-conditions.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#### Control flow based in boolean condition
# Control flow based in boolean condition

Always as possible, compare with a simple boolean (for example, in loops conditions): It's a more simple (and consecuentially faster):

```js
// slow
// two boolean conditions = 0 and > 0
// two boolean conditions: `= 0` and `> 0`
array.indexOf(i) >= 0

// fast
// just one boolean condition
// just one boolean condition: `!== 0`
array.indexOf(i) !== 0
```
10 changes: 5 additions & 5 deletions workflow/lookup-table.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#### Lookup table for result matching
# Lookup table for result matching

When we optimizing `if/else`, the goal is always to minimize the number of conditions to evaluate before taking the correct path. The easiest optimization is therefore to ensure that the most common conditions are first:
When we optimizing `if`/`else`, the goal is always to minimize the number of conditions to evaluate before taking the correct path. The easiest optimization is therefore to ensure that the most common conditions are first:

```js
if (value < 5) {
Expand All @@ -16,7 +16,7 @@ if (value < 5) {

Better use a direct match approach based in object/array index:

```
```js
var lookupTable = {
1: 'is greater',
-1: 'is less',
Expand All @@ -37,7 +37,7 @@ Also be careful about choose betweetn `Object` or `Array` for this purpose:

Under `ES2015`, consider use `Map` or `Set` combined with `Symbol`.

Althought is more readable, not in all scenarios use a lookup table is better: The cost of create the lookup table could be higher tan use a set of if/else statements. So, it depends about your code running time:
Althought is more readable, not in all scenarios use a lookup table is better: The cost of create the lookup table could be higher tan use a set of `if`/`else` statements. So, it depends about your code running time:

- If you have to handle a set of conditions (maybe less than 3 could be a metric) or the code running life is short, use if/else.
- If you have to handle a set of conditions (maybe less than 3 could be a metric) or the code running life is short, use `if`/`else`.
- In other case use lookup table solution.
Loading

0 comments on commit b7dbbb3

Please sign in to comment.