-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.js
More file actions
43 lines (38 loc) · 1.03 KB
/
Copy patharray.js
File metadata and controls
43 lines (38 loc) · 1.03 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
36
37
38
39
40
41
42
43
import { bench } from "benchik"
class asd { }
const f = new asd
const arrD = []
await bench.untilCompiled()
{
using _ = bench.group("Array Populate")
bench("dynamic (push)", () => {
const array = []
for (let i = 0; i < 1_000; i++) array.push(new asd)
return array
})
bench("dynamic (no push)", () => {
const array = []
for (let i = 0; i < 1_000; i++) array[i] = new asd
return array
})
bench("dynamic (no push via length)", () => {
const array = []
for (let i = 0; i < 1_000; i++) array[array.length] = new asd
return array
})
bench("pre-allocate", () => {
const array = Array(1_000)
for (let i = 0; i < 1_000; i++) array[i] = new asd
return array
})
bench("reusing", () => {
const array = arrD.fill(null, 0, 1000)
for (let i = 0; i < 1_000; i++) array[i] = new asd
return array
})
}
/**
* Outcome:
* - Pre-allocating an array of desired size gives free speed.
* - Reusing an array works only when array shouldn't be returned, used only as an intermediate.
*/