-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (73 loc) · 2.21 KB
/
Copy pathindex.js
File metadata and controls
76 lines (73 loc) · 2.21 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//test data
const count = 50 //items to be inserted
let items = []
for (let i = 0; i < count; i++) {
items[i] = `Paragraph ${i} of ${count}:
dummy text dummy text dummy text dummy text dummy text dummy text
dummy text dummy text dummy text dummy text dummy text dummy text
dummy text dummy text dummy text dummy text dummy text dummy text`
}
//html hooks
let container = document.getElementById('test-container')
let results = document.getElementById('results')
let status = document.getElementById('status')
function cleanup() {
container.innerHTML = ""
}
function resultLog(text) {
results.innerHTML += '<p>' + text + '</p>'
}
let Benchmark = require('benchmark')
global.Benchmark = Benchmark //somehow required otherwise lodash blows
let suite = new Benchmark.Suite;
// add tests
suite
.add('innerHTML', function () {
items.forEach((item, index) => {
container.innerHTML += `<p id="p-${index}">${item}</p>`
});
cleanup()
})
.add('insertAdjacentHTML', function () {
let allHTML = ""
items.forEach((item, index) => {
allHTML += `<p id="p-${index}">${item}</p>`
});
container.insertAdjacentHTML('beforeend', allHTML)
cleanup()
})
.add('appendChild', function () {
items.forEach((item, index) => {
let el = document.createElement("p")
el.textContent = item
el.id = "p-" + index
container.appendChild(el)
});
cleanup()
})
.add('DocumentFragments', function () {
let fragment = document.createDocumentFragment()
items.forEach((item, index) => {
let el = document.createElement("p")
el.textContent = item
el.id = "p-" + index
fragment.appendChild(el)
});
container.appendChild(fragment)
cleanup()
})
//add listeners
.on('start', function (event) {
resultLog(`Starting tests, inserting ${count} dummy <strong>p</strong> elements per cycle.`)
status.innerHTML = "Please wait..."
})
.on('cycle', function (event) {
resultLog(String(event.target))
// console.log(String(event.target))
})
.on('complete', function () {
resultLog('<strong>Fastest is ' + this.filter('fastest').map('name') + '</strong>')
status.innerHTML = "Completed."
})
// run async
.run({ 'async': true });