Skip to content

Commit 967976e

Browse files
committed
Add for in
1 parent 1a9f72e commit 967976e

File tree

4 files changed

+43
-30
lines changed

4 files changed

+43
-30
lines changed

README.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
11
# TypeScript-optimization
22
Tests and benchmarks different codes in TypeScript for different JavaScript versions (ES5 vs ES6 and above).
33

4-
### Traditional `for` vs `for-of` - Looping ovr Arrays
4+
### Traditional `for` vs `for-of` vs `for-in`- Looping ovr Arrays
55

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`.
88

99
```typescript
1010
// 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+
}
1515

1616
// 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+
}
1927
```
2028

21-
22-
The reason: probably because of `var` vs `let`.
23-
2429
<details>
2530
<summary>Benchmark-Result</summary>
2631

2732
ES2020:
2833

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)
3837
Fastest is for-traditional
3938

4039
ES 6:
4140

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)
4444
Fastest is for-traditional
4545

46-
4746
ES5:
4847

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
5252

5353
</details>

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
"benchmark": "^2.1.4",
77
"typescript": "^3.8.3"
88
},
9+
"devDependencies": {
10+
"prettier": "^1.19.1"
11+
},
912
"prettier": {
10-
"semi": false
13+
"semi": false,
14+
"printWidth": 120
1115
}
1216
}

src/for.ts renamed to src/for-array.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ suite.add("for-traditional", function() {
1414

1515
suite.add("for-of", function() {
1616
let sum = 0
17-
for (const n of arr) sum += n
17+
for (const a of arr) {
18+
sum += a
19+
}
20+
})
21+
22+
suite.add("for-in", function() {
23+
let sum = 0
24+
for (const i in arr) {
25+
sum += arr[i]
26+
}
1827
})
1928

2029
// add listeners

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"module": "commonjs",
4-
"target": "ES6", // I target ES5, ES6, and ES2020
4+
"target": "ES2020", // I target ES5, ES6, and ES2020
55
"sourceMap": false
66
},
77
"exclude": [

0 commit comments

Comments
 (0)