-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathObjectLiterals.ts
More file actions
35 lines (32 loc) · 1.04 KB
/
Copy pathObjectLiterals.ts
File metadata and controls
35 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Object-literal allocation benchmarks. Measures cost of `{ ... }` in
// hot loops — common in real code (return values, options bags, AST-like
// tree builders). Each iteration constructs a fresh Dictionary<string,
// object> and stores boxed values.
function smallLiteralLoop(n: number): number {
let total: number = 0;
for (let i: number = 0; i < n; i++) {
const o = { x: i, y: i + 1 };
total = total + (o.x as number);
}
return total;
}
function mediumLiteralLoop(n: number): number {
let total: number = 0;
for (let i: number = 0; i < n; i++) {
const o = { a: i, b: i + 1, c: i + 2, d: i + 3, e: i + 4 };
total = total + (o.a as number);
}
return total;
}
function nestedLiteralLoop(n: number): number {
let total: number = 0;
for (let i: number = 0; i < n; i++) {
const o = {
point: { x: i, y: i + 1 },
label: "pt"
};
const inner = o.point;
total = total + inner.x;
}
return total;
}