Skip to content

Commit fe30049

Browse files
committed
for-of optimization
1 parent 2346848 commit fe30049

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Benchmarks are done inside Atom (using script package) and Webstorm.
88
- ES6 and above: traditional `for` is **faster** than `for-of` that is faster than `for-in`
99
- ES5 and lower:Traditional `for` is **similar to** `for-of` and both faster than `for-in`.
1010

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+
1113
```typescript
1214
// Traditional
1315
let sum = 0
@@ -133,3 +135,46 @@ for (let i = 0; i < arr_return().length; ++i) {
133135
Fastest is for-traditional-const,for-traditional-length-lookup
134136

135137
</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 of arr) {
150+
sum += a
151+
}
152+
// for-of-full-array-lookup
153+
let sum = 0
154+
for (const a of arr_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)
178+
Fastest is for-of
179+
180+
</details>

src/for-of-array.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const Benchmark = require("benchmark")
2+
let suite = new Benchmark.Suite()
3+
4+
const arr: number[] = []
5+
for (let i = 0; i < 10000; ++i) arr.push(i)
6+
7+
// add tests
8+
suite.add("for-of", function() {
9+
let sum = 0
10+
for (const a of arr) {
11+
sum += a
12+
}
13+
})
14+
15+
function arr_return() {
16+
const arr2: number[] = []
17+
for (let i = 0; i < 10000; ++i) arr2.push(i)
18+
return arr2
19+
}
20+
21+
suite.add("for-of-full-lookup", function() {
22+
let sum = 0
23+
for (const a of arr_return()) {
24+
sum += a
25+
}
26+
})
27+
28+
// add listeners
29+
suite.on("cycle", function(event) {
30+
console.log(String(event.target))
31+
})
32+
suite.on("complete", function() {
33+
console.log("Fastest is " + this.filter("fastest").map("name"))
34+
})
35+
// run async
36+
suite.run({ async: true })

0 commit comments

Comments
 (0)