You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+45Lines changed: 45 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,8 @@ Benchmarks are done inside Atom (using script package) and Webstorm.
8
8
- ES6 and above: traditional `for` is **faster** than `for-of` that is faster than `for-in`
9
9
- ES5 and lower:Traditional`for` is **similar to**`for-of` and both faster than `for-in`.
10
10
11
+
If you notice, you see by targeting ES5 the TypeScript compiler converts `for-of` to the `traditional-for`, and that makes it faster than the original `for-of`!!
12
+
11
13
```typescript
12
14
// Traditional
13
15
let sum =0
@@ -133,3 +135,46 @@ for (let i = 0; i < arr_return().length; ++i) {
133
135
Fastest is for-traditional-const,for-traditional-length-lookup
134
136
135
137
</details>
138
+
139
+
140
+
### `for-of` optimization
141
+
142
+
- in all versions: full array look-up in the `for-head` is much slower.
143
+
144
+
If you notice, you see by targeting ES5 the TypeScript compiler converts `for-of` to the `traditional-for`, and that makes it faster than the original `for-of`!!
145
+
146
+
```typescript
147
+
// for-of
148
+
let sum =0
149
+
for (const a ofarr) {
150
+
sum+=a
151
+
}
152
+
// for-of-full-array-lookup
153
+
let sum =0
154
+
for (const a ofarr_return()) {
155
+
sum+=a
156
+
}
157
+
```
158
+
159
+
<details>
160
+
<summary>Benchmark-Result</summary>
161
+
162
+
ES2020:
163
+
164
+
for-of x 83,144 ops/sec ±0.52% (93 runs sampled)
165
+
for-of-full-lookup x 13,930 ops/sec ±0.62% (95 runs sampled)
166
+
Fastest is for-of
167
+
168
+
ES 6:
169
+
170
+
for-of x 83,036 ops/sec ±0.43% (95 runs sampled)
171
+
for-of-full-lookup x 13,779 ops/sec ±0.90% (96 runs sampled)
172
+
Fastest is for-of
173
+
174
+
ES5:
175
+
176
+
for-of x 110,799 ops/sec ±0.15% (96 runs sampled)
177
+
for-of-full-lookup x 15,122 ops/sec ±0.74% (95 runs sampled)
0 commit comments