Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy Chernitskiy committed Jul 12, 2021
1 parent 9893269 commit 76d7ad1
Show file tree
Hide file tree
Showing 11 changed files with 526 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
node_modules
.parcel-cache
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# worker-msg-test
# worker-msg-test

### run

parcel index.html
12 changes: 12 additions & 0 deletions index.html
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>
63 changes: 63 additions & 0 deletions index.ts
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);
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions package.json
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"
}
}
13 changes: 13 additions & 0 deletions src/config.ts
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,
];
Loading

0 comments on commit 76d7ad1

Please sign in to comment.