|
1 | 1 | # TypeScript-optimization
|
2 | 2 | Tests and benchmarks different codes in TypeScript for different JavaScript versions (ES5 vs ES6 and above).
|
3 | 3 |
|
4 |
| -### Traditional `for` vs `for-of` - Looping ovr Arrays |
| 4 | +### Traditional `for` vs `for-of` vs `for-in`- Looping ovr Arrays |
5 | 5 |
|
6 |
| -- Traditional `for` is **faster** than `for-of` for ES6 and above. |
7 |
| -- Traditional `for` is **similar to** `for-of` for ES5 (no meaningful difference). |
| 6 | +- ES6 and above: traditional `for` is **faster** than `for-of` that is faster than `for-in` |
| 7 | +- ES5 and lower:Traditional `for` is **similar to** `for-of` and both faster than `for-in`. |
8 | 8 |
|
9 | 9 | ```typescript
|
10 | 10 | // Traditional
|
11 |
| -let sum = 0 |
12 |
| -for (let i = 0, l = arr.length; i < l; ++i) { |
13 |
| -sum += arr[i] |
14 |
| -} |
| 11 | + let sum = 0 |
| 12 | + for (let i = 0, l = arr.length; i < l; ++i) { // Don't use `i < arr.length` instead |
| 13 | + sum += arr[i] |
| 14 | + } |
15 | 15 |
|
16 | 16 | // for - of
|
17 |
| -let sum = 0 |
18 |
| -for (const n of arr) sum += n |
| 17 | + let sum = 0 |
| 18 | + for (const a of arr) { |
| 19 | + sum += a |
| 20 | + } |
| 21 | + |
| 22 | +// for - in |
| 23 | + let sum = 0 |
| 24 | + for (const i in arr) { |
| 25 | + sum += arr[i] |
| 26 | + } |
19 | 27 | ```
|
20 | 28 |
|
21 |
| - |
22 |
| -The reason: probably because of `var` vs `let`. |
23 |
| - |
24 | 29 | <details>
|
25 | 30 | <summary>Benchmark-Result</summary>
|
26 | 31 |
|
27 | 32 | ES2020:
|
28 | 33 |
|
29 |
| - for-traditional x 110,906 ops/sec ±0.15% (94 runs sampled) |
30 |
| - for-of x 81,956 ops/sec ±1.09% (92 runs sampled) |
31 |
| - Fastest is for-traditional |
32 |
| - |
33 |
| - |
34 |
| - ES 2018: |
35 |
| - |
36 |
| - for-traditional x 110,582 ops/sec ±0.50% (95 runs sampled) |
37 |
| - for-of x 83,032 ops/sec ±0.55% (94 runs sampled) |
| 34 | + for-traditional x 111,162 ops/sec ±0.16% (96 runs sampled) |
| 35 | + for-of x 83,331 ops/sec ±0.15% (96 runs sampled) |
| 36 | + for-in x 2,544 ops/sec ±0.86% (94 runs sampled) |
38 | 37 | Fastest is for-traditional
|
39 | 38 |
|
40 | 39 | ES 6:
|
41 | 40 |
|
42 |
| - for-traditional x 110,783 ops/sec ±0.41% (97 runs sampled) |
43 |
| - for-of x 83,222 ops/sec ±0.25% (96 runs sampled) |
| 41 | + for-traditional x 110,622 ops/sec ±0.34% (95 runs sampled) |
| 42 | + for-of x 83,102 ops/sec ±0.16% (94 runs sampled) |
| 43 | + for-in x 2,521 ops/sec ±1.21% (94 runs sampled) |
44 | 44 | Fastest is for-traditional
|
45 | 45 |
|
46 |
| - |
47 | 46 | ES5:
|
48 | 47 |
|
49 |
| - for-traditional x 110,915 ops/sec ±0.28% (92 runs sampled) |
50 |
| - for-of x 111,122 ops/sec ±0.20% (93 runs sampled) |
51 |
| - Fastest is for-of |
| 48 | + for-traditional x 110,584 ops/sec ±0.56% (95 runs sampled) |
| 49 | + for-of x 110,395 ops/sec ±0.60% (98 runs sampled) |
| 50 | + for-in x 2,604 ops/sec ±1.05% (95 runs sampled) |
| 51 | + Fastest is for-traditional,for-of |
52 | 52 |
|
53 | 53 | </details>
|
0 commit comments