Skip to content

Commit

Permalink
Fixed wording in /workflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
kerwitz committed Jun 14, 2017
1 parent 2f1a461 commit a39ce74
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 28 deletions.
8 changes: 4 additions & 4 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
* [Bibliography](bibliography.md)
* [Resources](resources.md)
* [Changelog](changelog.md)

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

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

* Number
* [Parsing string](number/parse-string.md)

Expand All @@ -34,6 +34,6 @@
* Workflow
* [Lookup table](workflow/lookup-table.md)
* [Math methods](workflow/math.md)
* [Memoization](workflow/memoization.md)
* [Memorization](workflow/memorization.md)
* [Scope](workflow/scope.md)
* [Variable access](workflow/variable-access.md)
* [Variable access](workflow/variable-access.md)
17 changes: 10 additions & 7 deletions docs/workflow/lookup-table.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Lookup table

When optimizing `if`/`else`, we want minimize the number of conditions to evaluate. This applies to `switch` cases as well.
When optimizing `if`/`else`, we want to minimize the number of conditions to
evaluate. This applies to `switch` cases as well.

The easiest optimization is to ensure that the most common conditions are first:

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

## Index lookups

It's sometimes better to use a direct match approach using object/array index lookups:
Sometimes it is better to use a direct match approach using object/array index lookups:

```js
var lookupTable = {
Expand All @@ -30,18 +31,20 @@ var myValue = 5
lookupTable[compare(4, myValue)]
```

In objects, you may also use *falsy* values as keys.
You may also use *falsy* values as keys in objects.

## Array vs. Object

Be careful about choosing between `Object` or `Array`:
Be careful when choosing between `Object` or `Array`:

- If you need a incremental index, use `Array`.
- If you need an incremental index, use `Array`.
- In other cases, use `Object`.

## Caveats

Although it is more readable, using a lookup table isn't always better. The cost of creating the lookup table could be higher than using a set of `if`/`else` statements. So, it depends on your code:
Although it is more readable, using a lookup table isn't always better. The cost
of creating the lookup table could be higher than using a set of `if`/`else`
statements. It depends on your code:

- If you have to handle a small set of conditions (maybe less than 3) or the code runtime life is short, use `if`/`else`.
- In other cases, use a lookup table.
- In other cases use a lookup table.
7 changes: 5 additions & 2 deletions docs/workflow/math.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Math methods

Native parts of JavaScript are all written in a lower-level language such as C++. That means these methods are compiled down to machine code as part of the browser and therefore don't have the same limitations as your code.
Native parts of JavaScript are all written in a lower-level language such as C++.
That means these methods are compiled down to machine code as part of the browser
and therefore don't have the same limitations as your code.

The `Math` object contains properties and methods designed to make mathematical operations easier and most of the time they are implemented as native operations.
The `Math` object contains properties and methods designed to make mathematical
operations easier and most of the time they are implemented as native operations.

There are several mathematical constants available:

Expand Down
24 changes: 24 additions & 0 deletions docs/workflow/memorization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Memorization

Just calculate the value of something once and reuse the value. This avoids the
cost of recalculating the same value, caching successive calls:

```js
var cache = Object.create(null)

var cityOne = 'Murcia'
var cityTwo = 'Madrid'
var routeName = `${cityOne}${cityTwo}`

if (!cache[routeName]) cache[routeName] = getDistance(cityOne, cityTwo)
```

Notes the first line: `Object.create(null)`. This create an `Object` without
`prototype`. because we want a pure object to be used as a hash table (with all
the [prototypical methods](https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Object/prototype) missing).

If you need more control of your cache, you can create it using Map or WeakMap
structures. Specially use this when you want to remove items from the cache.

[LRU](https://www.npmjs.com/package/lru) or [mem](https://www.npmjs.com/package/mem)
are good high level libraries that implement this technique.
28 changes: 21 additions & 7 deletions docs/workflow/scope.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# Scope

If you declare a global variable but you only use it in a small part of your code, then you are keeping the variable in memory all the time.
If you declare a global variable but you only use it in a small part of your code,
then you are keeping the variable in memory all the time.

When a variable is referenced, Javascript hunts it down by looping through the different members of the scope chain. This scope chain is the set of variables available within the current scope, and across all major browsers it has at least two items: a set of local variables and a set of global variables.
When a variable is referenced, Javascript hunts it down by looping through the
different members of the scope chain. This scope chain is the set of variables
available within the current scope. Across all major browsers it has at least
two items: a set of local variables and a set of global variables.

Simply put, the deeper the engine has to dig into this scope chain, the longer the operation will take. It first goes through the local variables starting with `this`, the function arguments, then any locally defined variables, and afterwards it iterates through global variables.
Simply put, the deeper the engine has to dig into this scope chain, the longer
the operation will take. It first goes through the local variables starting with
`this`, the function arguments, then any locally defined variables, and afterwards
it iterates through global variables.

## const & let

Expand All @@ -28,12 +35,19 @@ const objt = {}
// => TypeError: Identifier 'objt' has already been declared
```

!> If you want to use `const` over `Object` and also be not possible reassign object properties, use [Object.freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze)
!> If you want to use `const` over `Object` and also be not possible reassign
object properties, use [Object.freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze)

`let` means that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it’s defined in, which is not always the entire containing function.
`let` means that the variable may be reassigned, such as a counter in a loop, or
a value swap in an algorithm. It also signals that the variable will be used only
in the block it’s defined in, which is not always the entire containing function.

`var` is now the weakest signal available when you define a variable in JavaScript. The variable may or may not be reassigned, and the variable may or may not be used for an entire function, or just for the purpose of a block or loop.
`var` is now the weakest signal available when you define a variable in JavaScript.
The variable may or may not be reassigned, and the variable may or may not be used
for an entire function, or just for the purpose of a block or loop.

## A Simple rule

So if you need to allow the variable to be reassigned, use `let`, otherwise use `const`. Also, if you need to deallocate a value by unsetting it, you may consider `let` over `const`.
So if you need to allow the variable to be reassigned, use `let`, otherwise use
`const`. Also, if you need to deallocate a value by unsetting it, you may consider
`let` over `const`.
18 changes: 10 additions & 8 deletions docs/workflow/variable-access.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Variable access

This tip is related with access with a deeply a frequently referenced path of an `Array` or `Object`.

When it comes to Javascript data, there’s pretty much four ways to access it:
When it comes to Javascript data, there’s pretty much four ways to access it:

- Literal values
- Variables
- Object properties
- Array items

When thinking about optimization, literal values and variables perform about the same, and are significantly faster than object properties and array items.
When thinking about optimization, literal values and variables perform about the
same, and are significantly faster than object properties and array items.

So whenever you reference an object property or array item multiple times, you can get a performance boost by defining a variable. (This applies to both reading and writing data).
So whenever you reference an object property or array item multiple times, you
can get a performance boost by defining a variable (this applies to both reading and writing data).

```js
var name = myObject.name
Expand All @@ -21,7 +21,7 @@ var value = array[3]
Consider this loop:

```js
// minimizing property lookups
// Minimizing property lookups
for (var i = 0, num = items.length; i < num; i++) process(items[i])
```

Expand All @@ -41,7 +41,8 @@ do process(items[k++])
while (k < num)
```

Depending on the length of the array, you can save around 25% off the total loop execution time in most engines.
Depending on the length of the array, you can save around 25% off the total loop
execution time in most engines.

!> Decreasing the work done per iteration is most effective when the loop has a complexity of O(n). When the loop is more complex than O(n), it is advisable to focus your attention on decreasing the number of iterations.

Expand All @@ -57,4 +58,5 @@ while (j--) {

Now you are using one variable for the control and based in a boolean value. Excellent!

By reversing loops and minimizing property lookups, you can see execution times that are up to 50%–60% faster than the original.
By reversing loops and minimizing property lookups, you can see execution times
that are up to 50%–60% faster than the original.

0 comments on commit a39ce74

Please sign in to comment.