-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timing.html
151 lines (112 loc) · 3 KB
/
Timing.html
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="icon" href="https://maeyler.github.io/favicon.ico">
<title>Timing</title>
<style>
body {
max-width: 450px;
}
#clock {
font-size: 48px;
background-color: blue;
color: white;
text-align: center;
margin-top: 20px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<button onclick="init()">Array vs Map</button>
<div id="result"></div>
<script>
"use strict";
class Course {
constructor(name, time, date) {
this.name = name
this.time = time
this.date = date
this.rooms = []
}
addRooms(...room) {
for (let r of room) this.rooms.push(r)
return this
}
toString() {
return this.name;
}
}
class Student {
constructor(id, name, gpa) {
this.id = id
this.name = name
this.gpa = gpa
this.courses = []
}
addCourses(...course) {
for (let c of course) this.courses.push(c)
return this
}
toString() {
return this.name +": "+this.gpa;
}
}
let randomID
const url = "https://maeyler.github.io/JS/data/"
const array = [] // Student ids
const map = new Map() // id to student
async function readData(file) {
let data = await fetch(url + file)
let text = await data.text()
addStudents(text)
}
function parseStudent(line) {
let [id, name, gpa, ...L] = line.split('\t')
let std = new Student(id, name, gpa)
std.courses = L; return std
}
function addStudents(txt) {
let a = txt.split('\n')
for (let s of a) {
let std = parseStudent(s)
array.push(std.id); map.set(std.id, std);
}
}
function searchArr() {
let foundinArray = 0
for (let i = 0; i < 10000; i++) {
if (array.includes(randomID)) {
foundinArray++
}
}
return foundinArray
}
function searchMap() {
let foundinMap = 0
for (let i = 0; i < 10000; i++) {
if (map.has(randomID)) {
foundinMap++
}
}
return foundinMap
}
function timing(f) {
let t = performance.now() //more accurate than Date.now()
let x = f()
t = performance.now() - t
let s = f.name + ": " + t.toPrecision(3) + " msec -- count:" + x + '\n'
result.innerText += s;
}
readData("Students.txt")
function init() {
result.innerText = ""
randomID = Math.floor(Math.random() * 1000000000)
timing(searchArr)
timing(searchMap)
}
</script>
</body>
</html>