forked from vitaliy-gis/worker-msg-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vitaliy Chernitskiy
committed
Jul 12, 2021
1 parent
9893269
commit 76d7ad1
Showing
11 changed files
with
526 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
dist | ||
node_modules | ||
.parcel-cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
# worker-msg-test | ||
# worker-msg-test | ||
|
||
### run | ||
|
||
parcel index.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Wroker test</title> | ||
</head> | ||
<body> | ||
<script src="./index.ts"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { unpackJson } from './src/json2pbf'; | ||
import { ITERATIONS_COUNT, JSON_TEMPLATE } from './src/config'; | ||
|
||
const worker = new Worker(new URL('./src/worker.ts', import.meta.url)); | ||
const decoder = new TextDecoder('utf-8'); | ||
async function makeTests() { | ||
function waitResults(type: number, unpack: (data: any) => { data: any; hash: string }) { | ||
const results: any[] = []; | ||
const converted: any[] = []; | ||
return new Promise<void>((res) => { | ||
let count = 0; | ||
|
||
worker.onmessage = (evt) => { | ||
if (count === 0) { | ||
console.time('receive'); | ||
} | ||
count++; | ||
results.push(evt.data.data); | ||
if (count === ITERATIONS_COUNT) { | ||
console.timeEnd('receive'); | ||
console.time('unpack'); | ||
for (let i = 0; i < ITERATIONS_COUNT; i++) { | ||
converted.push(unpack(results[i])); | ||
} | ||
console.timeEnd('unpack'); | ||
// выводим последний элемент что бы проверить на валидность | ||
console.log(converted[converted.length - 1]); | ||
setTimeout(res, 300); | ||
} | ||
}; | ||
worker.postMessage(type); | ||
}); | ||
} | ||
console.log('Массив ', JSON_TEMPLATE); | ||
console.log(`Передаем из воркера в главный поток ${ITERATIONS_COUNT} раз`); | ||
console.group('передаем типизированный массив напрямую'); | ||
await waitResults(0, (data) => ({ data, hash: data.toString() })); | ||
console.groupEnd(); | ||
|
||
console.group('передаем объект напрямую'); | ||
await waitResults(1, (data) => ({ data, hash: JSON.stringify(data) })); | ||
console.groupEnd(); | ||
|
||
console.group('передаем как строку'); | ||
await waitResults(2, (data) => ({ data: JSON.parse(data), hash: data })); | ||
console.groupEnd(); | ||
|
||
console.group('JSON.parse + TextDecoder'); | ||
await waitResults(3, (data) => { | ||
const hash = decoder.decode(data); | ||
return { data: JSON.parse(hash), hash }; | ||
}); | ||
console.groupEnd(); | ||
|
||
console.group('PBF'); | ||
await waitResults(4, (data) => { | ||
const unpacked = unpackJson(data); | ||
return { data: unpacked, hash: JSON.stringify(unpacked) }; | ||
}); | ||
console.groupEnd(); | ||
} | ||
|
||
setTimeout(makeTests, 300); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "worker-msg-test", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.html", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/vitaliy-gis/worker-msg-test.git" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/vitaliy-gis/worker-msg-test/issues" | ||
}, | ||
"homepage": "https://github.com/vitaliy-gis/worker-msg-test#readme", | ||
"dependencies": { | ||
"pbf": "^3.2.1", | ||
"tslib": "^2.3.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export const ITERATIONS_COUNT = 10000; | ||
export const JSON_TEMPLATE = [ | ||
2, | ||
769, | ||
0.00000011, | ||
NaN, | ||
NaN, | ||
10000, | ||
'#ffffff', | ||
true, | ||
'#ff0000', | ||
null, | ||
]; |
Oops, something went wrong.