-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloops-test-array.mjs
More file actions
62 lines (51 loc) · 1.23 KB
/
loops-test-array.mjs
File metadata and controls
62 lines (51 loc) · 1.23 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { trackAveragePerformance } from './performanceWrapper.mjs'
const propsNumber = 1000 * 1000 * 10
const arrayTest = []
const filler = () => {
for (let index = 0; index < propsNumber; index++) {
const value = `value${index + 1}`
arrayTest.push(value)
}
}
filler()
// ! Always better at large collections
const executeTestCase1 = () => {
const cache = []
for (let index = 0; index < arrayTest.length; index++) {
cache.push(arrayTest[index])
}
}
// medium speed, not recommended
const executeTestCase2 = () => {
const cache = []
arrayTest.forEach(element => {
cache.push(element)
});
}
// ! Better at small collections
const executeTestCase3 = () => {
const cache = []
for (const iterator of arrayTest) {
cache.push(iterator)
}
}
const executeTestCase4 = () => {
const cache = []
for (const iterator in arrayTest) {
cache.push(iterator)
}
}
const executeTestCase5 = () => {
const cache = arrayTest.map(e => e)
}
const cases = {
'FOR': executeTestCase1,
'ForEach': executeTestCase2,
'FOR OF': executeTestCase3,
'FOR In': executeTestCase4,
'Map': executeTestCase5,
}
Object.entries(cases).forEach(([n, c]) => {
console.log(n)
trackAveragePerformance(c, n, 'heapUsed', 10)
})