Skip to content

Commit e86b527

Browse files
committed
feat: rewrite and add new sections
1 parent 07e87db commit e86b527

20 files changed

+258
-156
lines changed

.travis.yml

Lines changed: 0 additions & 28 deletions
This file was deleted.
File renamed without changes.

docs/_sidebar.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,28 @@
1616

1717
* Function
1818
* [new agnostic](function/new.md)
19-
* [bind is slower](function/bind.md)
19+
* [.bind is slower](function/bind.md)
2020

2121
* RegExp
2222
* [Use the correct method](regexp/correct-methods.md)
2323
* [Focus on failing faster](regexp/fail-faster.md)
24-
* [Mandatory Fields](regexp/flags.md)
24+
* [Dot all flag](regexp/dot-all-flag.md)
25+
* [Global flag](regexp/global-flag.md)
26+
* [Unicode flag](regexp/unicode-flag.md)
27+
28+
* Object
29+
* [Empty prototype](object/empty-prototype.md)
2530

2631
* V8
27-
* [Hidden Class](v8-tips/hidden-class.md)
28-
* [Sparse Arrays](v8-tips/sparse-arrays.md)
29-
* [Properties Names](v8-tips/properties-names.md)
3032
* [Float Number](v8-tips/float-number.md)
33+
* [Freeing memory](v8-tips/freeing-memory.md)
34+
* [Hidden classes](v8-tips/hidden-classes.md)
3135
* [Inline initialization](v8-tips/inline-initialization.md)
3236
* [Monomorphic](v8-tips/monomorphic.md)
37+
* [Properties Names](v8-tips/properties-names.md)
38+
* [Sparse Arrays](v8-tips/sparse-arrays.md)
3339
* [Use strict](v8-tips/use-strict.md)
34-
* [Freeing memory](v8-tips/freeing-memory.md)
40+
* [Spread Syntax](v8-tips/spread-syntax.md)
3541

3642
* Workflow
3743
* [Deferring by a tick](workflow/defer.md)
@@ -42,4 +48,4 @@
4248
* [Scope](workflow/scope.md)
4349
* [Variable access](workflow/variable-access.md)
4450
* [How to clone](workflow/how-to-clone.md)
45-
* [null and undefined](workflow/null-and-undefined.md)
51+
* [null or undefined](workflow/null-or-undefined.md)

docs/array/pop-or-shift.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# pop over shift
1+
# .pop over .shift
22

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

docs/array/preallocation.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Preallocation
22

3-
## Initialize value
3+
Preallocation is when you reserve memory space before using it.
4+
5+
This is specially useful when you know much much space you need, or you want to avoid expanding repeatedly the size of a collection over time.
6+
7+
## Initializing the values
48

59
If you want to preallocate an array with a initial value, you can use [`Array.from()`](https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Array/from):
610

@@ -11,7 +15,7 @@ const n = 5
1115
[...Array(n).keys()] // [ 0, 1, 2, 3, 4 ]
1216

1317
// more powerful
14-
Array.from({length: n}, (value, index) => index) // [ 0, 1, 2, 3, 4 ]
18+
Array.from({ length: n }, (value, index) => index) // [ 0, 1, 2, 3, 4 ]
1519
```
1620

1721
## Reusing instances

docs/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
<script src="//unpkg.com/docsify@latest/lib/plugins/external-script.min.js"></script>
4949
<script src="//unpkg.com/docsify@latest/lib/plugins/ga.min.js"></script>
5050
<script src="//unpkg.com/docsify-pagination/dist/docsify-pagination.min.js"></script>
51-
<script async src="//cdn.headwayapp.co/widget.js"></script>
5251
<script src="//cdn.jsdelivr.net/npm/codecopy/umd/codecopy.min.js"></script>
5352

5453
</html>

docs/main.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/object/empty-prototype.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Empty prototype
2+
3+
All objects in JavaSCript are instance of `Object` and they inherit properties and methods, such as `.toString`.
4+
5+
There are situations in which you just want to use an `Object` instance as a container of values.
6+
7+
For thoese cases, an `Object` maybe be deliberately created with an empty prototype, meaning it inherits nothing.
8+
9+
```js
10+
const cache = Object.create(null)
11+
```
12+
13+
In this way, the object has a smaller memory footprint.
14+
15+
Since it doesn't inherit from other object, you can't typechecking using `instanceof`:
16+
17+
```js
18+
Object.create(null) instanceof Object // false
19+
```

docs/regexp/flags.md renamed to docs/regexp/dot-all-flag.md

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,4 @@
1-
# Always use flags
2-
3-
## Unicode flag
4-
5-
The unicode (`u`) flag is mandatory when working with Unicode strings, in particular when you might need to handle characters in astral planes, the ones that are not included in the first 1600 Unicode characters.
6-
7-
Like Emojis, for example, but not just those.
8-
9-
If you don’t add that flag, this simple regex that should match one character will not work, because for JavaScript that emoji is represented internally by 2 characters (see [Unicode in JavaScript](https://flaviocopes.com/javascript-unicode/)):
10-
11-
```js
12-
/^.$/.test('a') //
13-
/^.$/.test('🐶') //
14-
/^.$/u.test('🐶') //
15-
```
16-
17-
So, always use the `u` flag.
18-
19-
Keep in mind that Unicode, just normal characters, handle ranges:
20-
21-
```js
22-
/[a-z]/.test('a') //
23-
/[1-9]/.test('1') //
24-
25-
/[🐶-🦊]/u.test('🐺') //
26-
/[🐶-🦊]/u.test('🐛') //
27-
```
28-
29-
JavaScript checks the internal code representation, so `🐶 < 🐺 < 🦊` because `\u1F436 < \u1F43A < \u1F98A`. Check [emoji-regex](https://github.com/mathiasbynens/emoji-regex) for exploring more about that.
30-
31-
## Dot all flag
1+
# Dot all flag (s)
322

333
By default, `.` matches any character except for line terminators.
344

@@ -38,25 +8,25 @@ Line terminators are specially remarkable when you are working with [Templates S
388
const input = `
399
Lorem ipsum dolor sit amet, consectetur adispiscing hello
4010
world elit. Nam sit amet elit id risus aliquam porta.
41-
`
11+
`;
4212

4313
/hello.world/u.test(input)
4414
// → false 🤔
4515
```
4616

4717
As you can see, it doesn't match because a line break is present between `hello` and `world` words.
4818

49-
The problem, is that the dot, in fact, doest not math all characters. It only matches characters that JavaScript does not consider to be line terminators.
19+
The "dot" doesn't match all characters. It only matches characters that JavaScript doesn't consider to be line terminators.
5020

5121
Sometimes you really do want to match any character, including new lines. This problem is so common that developers have started to use workarounds like this:
5222

5323
```js
5424
const input = `
5525
Lorem ipsum dolor sit amet, consectetur adispiscing hello
5626
world elit. Nam sit amet elit id risus aliquam porta.
57-
`
27+
`;
5828

59-
/hello[\s\S]world/u.test(input);
29+
/hello[\s\S]world/u.test(input)
6030
// → true 😕
6131
```
6232

@@ -68,9 +38,9 @@ Another workaround is to use a negated empty character class:
6838
const input = `
6939
Lorem ipsum dolor sit amet, consectetur adispiscing hello
7040
world elit. Nam sit amet elit id risus aliquam porta.
71-
`
41+
`;
7242

73-
/hello[^]world/u.test(input);
43+
/hello[^]world/u.test(input)
7444
// → true 🤷‍♂️
7545
```
7646

@@ -84,7 +54,7 @@ The new regular expression flag called dotAll mode (`s`) makes the dot truly mat
8454
const input = `
8555
Lorem ipsum dolor sit amet, consectetur adispiscing hello
8656
world elit. Nam sit amet elit id risus aliquam porta.
87-
`
57+
`;
8858

8959
/hello.world/us.test(input)
9060
// → true 🎉

docs/regexp/global-flag.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Global flag (g)
2+
3+
The global flag indicates the regular expression should be tested against all possible matches in a string.
4+
5+
```js
6+
const regex = new RegExp('o', 'g')
7+
8+
const greetings = 'Hello, how are you?'
9+
const results = greetings.match(regex)
10+
11+
console.log(results.length) // => 3
12+
```
13+
14+
If you just want to know if a string matches a regular expression, you use [RegExp.test()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test) for it.
15+
16+
```js
17+
const regex = new RegExp('o', 'g')
18+
const greetings = 'Hello, how are you?'
19+
20+
regex.test(greetings) // => true
21+
```
22+
23+
When `.test` is invoked, the regex keeps internally the state of the search at [lastIndex](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) property.
24+
25+
This will be cause inconsistent results if you call test method several times:
26+
27+
```js
28+
const regex = new RegExp('o', 'g')
29+
const greetings = 'Hello, how are you?'
30+
31+
regex.lastIndex // => 0
32+
regex.test(greetings) // => true
33+
regex.lastIndex // => 5
34+
regex.test(greetings) // => true
35+
regex.lastIndex // => 9
36+
regex.test(greetings) // => true
37+
regex.lastIndex // => 17
38+
regex.test(greetings) // => false
39+
regex.lastIndex // => 0
40+
```
41+
42+
As you can see, `lastIndex` will continue from the last matched result.
43+
44+
That can be easily avoided just wrapping into a function that will be executed from the beginning every time:
45+
46+
```js
47+
const regex = new RegExp('o', 'g')
48+
const greetings = 'Hello, how are you?'
49+
50+
const test = (regex, str) => regex.test(str)
51+
52+
test(regex, greetings) // => true
53+
```

0 commit comments

Comments
 (0)