-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsSortingScript.js
49 lines (46 loc) · 1.21 KB
/
jsSortingScript.js
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
/**
* Just trying to do some stuff that would block the main thread from running to test out web workers
*
* Eventually want to clean up and have a sorting function or something that is expensive enought and can be replicated in Go, to show off WebAssembly
*
*/
const startTime = new Date().toLocaleTimeString();
console.log("PARTYTOWN", "starting at " + startTime);
const data = fetch("https://pokeapi.co/api/v2/pokemon")
.then((resp) => {
return resp.json();
})
.then((data) => {
console.log("PARTYTOWN", data.results);
const results = data.results;
let duplicated = [];
let counter = 0;
while (counter < 3000) {
duplicated = [...duplicated, ...results];
counter++;
}
return duplicated;
})
.then((duplicated) => {
console.log(
"DUPLICATED 2 at ",
new Date().toLocaleTimeString(),
duplicated
);
sortPokemon(duplicated);
})
.then(() => {
const endTime = new Date().toLocaleTimeString();
console.log("PARTYTOWN", "done at " + endTime);
});
function sortPokemon(pokemon) {
return pokemon.sort(function (a, b) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
});
}