### Electron Broadcast Channel Example
Source: https://github.com/pubkey/broadcast-channel/blob/master/test-electron/index.html
This snippet demonstrates how to set up and use a Broadcast Channel in an Electron application. It shows how to require another JavaScript file to run in the same process, implying inter-process communication setup.
```javascript
require('./page.js')
```
--------------------------------
### Performance Metrics Snapshot (13.11.2019)
Source: https://github.com/pubkey/broadcast-channel/blob/master/perf.txt
A snapshot of performance metrics recorded on November 13, 2019, detailing 'openClose', 'sendRecieve' (parallel and series).
```json
{
"openClose": 565.4872879981995,
"sendRecieve": {
"parallel": 181.46631100028753,
"series": 2321.6348760016263
}
}
```
--------------------------------
### Build Size Optimization
Source: https://github.com/pubkey/broadcast-channel/blob/master/perf.txt
Tracks the reduction in build size over several iterations. Each 'AFTER' state represents an optimization step, showing a consistent decrease in the final build size.
```bash
====== build-size
BEFORE: 37251
AFTER: 4077
====== build-size 2
BEFORE: 4077
AFTER: 3795
====== build-size 3
BEFORE: 3795
AFTER: 3110
```
--------------------------------
### Performance Metrics Snapshot (3.12.2021)
Source: https://github.com/pubkey/broadcast-channel/blob/master/perf.txt
Performance metrics recorded on December 3, 2021, including 'openClose', 'sendRecieve' (parallel and series), and 'leaderElection'. Two distinct sets of metrics are provided for this date.
```json
{
"openClose": 1110.2557100057602,
"sendRecieve": {
"parallel": 279.0764960050583,
"series": 2797.712993979454
},
"leaderElection": 2122.0940190553665
}
```
```json
{
"openClose": 885.0331689119339,
"sendRecieve": {
"parallel": 279.1763379573822,
"series": 2232.475461959839
},
"leaderElection": 2150.9966419935226
}
```
--------------------------------
### Performance: New Unload Module
Source: https://github.com/pubkey/broadcast-channel/blob/master/perf.txt
Compares performance metrics before and after introducing a new unload module. Shows changes in 'openClose' and 'sendRecieve' (parallel and series) operations.
```json
{
"openClose": 765.1154530011117,
"sendRecieve": {
"parallel": 259.89112799987197,
"series": 4052.1648419983685
}
}
```
```json
{
"openClose": 672.2327830009162,
"sendRecieve": {
"parallel": 250.701522000134,
"series": 4001.1675169989467
}
}
```
--------------------------------
### Build Size Optimization (No Default Import)
Source: https://github.com/pubkey/broadcast-channel/blob/master/perf.txt
Measures the build size before and after removing a default import. The change results in a minimal increase in build size.
```bash
no default import
npm run build:size
BEFORE: 112358
AFTER : 112401
```
--------------------------------
### IndexedDB Performance Comparison
Source: https://github.com/pubkey/broadcast-channel/blob/master/perf.txt
Compares IndexedDB performance before and after implementing a localstorage ping mechanism. The results show a slight increase in latency when using localstorage pings.
```json
BEFORE:
3610
3380
3489
AFTER (with localstorage ping):
4183
3962
3821
```
--------------------------------
### Message Send/Receive Performance (General)
Source: https://github.com/pubkey/broadcast-channel/blob/master/perf.txt
A series of performance tests comparing 'before' and 'after' states for message operations. Each 'after' state reflects a different optimization strategy, such as implementing a paths cache, cleanup, parallel execution, parallel content reading, and improved postMessage handling, all showing performance gains.
```json
BEFORE: {
"openClose": 1499.9152579996735,
"sendRecieve": {
"parallel": 6752.695256000385,
"series": 5142.3914529997855
}
}
AFTER: { // getPathsCache
"openClose": 1154.4196130000055,
"sendRecieve": {
"parallel": 6559.061360999942,
"series": 4965.728401999921
}
}
AFTER2: { // cleanup things
"openClose": 1086.149023000151,
"sendRecieve": {
"parallel": 6496.672225000337,
"series": 4932.777033999562
}
}
AFTER3: { // run things in parallel
"openClose": 737.8487470000982,
"sendRecieve": {
"parallel": 6637.516607999802,
"series": 4835.849313000217
}
}
AFTER4: { // read content in parallel
"openClose": 746.0398439988494,
"sendRecieve": {
"parallel": 6332.704676998779,
"series": 4761.053835000843
}
}
AFTER5: { // better postMessage
"openClose": 666.0222460012883,
"sendRecieve": {
"parallel": 5854.225347001106,
"series": 4425.243154998869
}
}
```
--------------------------------
### Performance: Cache ensure-folder exists
Source: https://github.com/pubkey/broadcast-channel/blob/master/perf.txt
Assesses performance impact of ensuring a folder exists within the caching mechanism. Compares 'BEFORE' and 'AFTER' states for 'openClose', 'sendRecieve' (parallel and series).
```json
{
"openClose": 583.2204679995775,
"sendRecieve": {
"parallel": 206.64538500085473,
"series": 3861.489134998992
}
}
```
```json
{
"openClose": 544.0778630003333,
"sendRecieve": {
"parallel": 220.51885700039566,
"series": 2255.5608139988035
}
}
```
--------------------------------
### Performance: Cache tmp-folder
Source: https://github.com/pubkey/broadcast-channel/blob/master/perf.txt
Evaluates performance changes related to caching a temporary folder. Includes multiple 'BEFORE' states and one 'AFTER' state for 'openClose', 'sendRecieve' (parallel and series).
```json
{
"openClose": 611.5843779994175,
"sendRecieve": {
"parallel": 229.9967109998688,
"series": 4040.1850410001352
}
}
```
```json
{
"openClose": 586.3366490006447,
"sendRecieve": {
"parallel": 237.4793659998104,
"series": 3972.603798000142
}
}
```
```json
{
"openClose": 563.7609900003299,
"sendRecieve": {
"parallel": 233.5304539995268,
"series": 3869.6750210002065
}
}
```
--------------------------------
### BroadcastChannel Demo HTML Structure
Source: https://github.com/pubkey/broadcast-channel/blob/master/docs/index.html
This snippet defines the basic HTML structure for the BroadcastChannel demo page, including input fields, buttons, and a message display area.
```html
BroadcastChannel Demo
```
--------------------------------
### Performance: Native Node.js Code
Source: https://github.com/pubkey/broadcast-channel/blob/master/perf.txt
Compares performance metrics when using native Node.js code without transpilation versus a previous state. Focuses on 'openClose' and 'sendRecieve' (parallel and series) operations.
```json
{
"openClose": 733.8300310000777,
"sendRecieve": {
"parallel": 245.27187200076878,
"series": 3865.821045000106
}
}
```
```json
{
"openClose": 577.4335329998285,
"sendRecieve": {
"parallel": 226.03592699952424,
"series": 4163.729206999764
}
}
```
--------------------------------
### BroadcastChannel Live Test Page Structure
Source: https://github.com/pubkey/broadcast-channel/blob/master/docs/e2e.html
This snippet represents the HTML structure of the live test page, including CSS for layout and styling, and placeholders for test controls and status updates.
```html
body { margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; }
#header { position: fixed; height: 140px; width: 99%; padding-left: 1%; border-bottom-style: solid; background-color: white; -webkit-box-shadow: 0px 4px 18px 0px rgba(0, 0, 0, 0.75); -moz-box-shadow: 0px 4px 18px 0px rgba(0, 0, 0, 0.75); box-shadow: 0px 4px 18px 0px rgba(0, 0, 0, 0.75); margin: 0; font-size: 12px; }
#header p { margin: 0; }
#kirby { float: right; margin-right: 150px; }
#header h2 { margin: 0; margin-top: 10px; font-size: 15px; padding: 0; }
select, button { cursor: pointer; }
.state-box { margin-top: 150px; float: left; width: 96%; margin-left: 1%; padding: 1%; border-style: solid; background-color: white; }
.msg-box { background-color: white; width: 400px; float: right; height: 500px; overflow-y: scroll; }
```
--------------------------------
### Message Send/Receive Performance (Write Message Up Front)
Source: https://github.com/pubkey/broadcast-channel/blob/master/perf.txt
Compares message handling performance before and after implementing a 'write message up front' optimization. This change significantly reduces the latency for parallel message reception.
```json
BEFORE: {
"openClose": 714.9132689982653,
"sendRecieve": {
"parallel": 6018.035248000175,
"series": 4019.5094799995422
}
}
AFTER: { // write message up front
"openClose": 703.9341719998047,
"sendRecieve": {
"parallel": 233.59367400035262,
"series": 4531.717969999649
}
}
```
--------------------------------
### Message Send/Receive Performance (July 14, 2018)
Source: https://github.com/pubkey/broadcast-channel/blob/master/perf.txt
Performance metrics for message open/close and send/receive operations (parallel and series) before and after optimizations. The 'after' state shows significant improvements in both parallel and series message handling.
```json
before: {
"openClose": 1589.1032320000231,
"sendRecieve": {
"parallel": 8576.14631400071,
"series": 8902.407701000571
}
}
after: {
"openClose": 1606.3578069992363,
"sendRecieve": {
"parallel": 6627.974293999374,
"series": 5202.203781999648
}
}
```
--------------------------------
### Message Send/Receive Performance (Add Fast Path)
Source: https://github.com/pubkey/broadcast-channel/blob/master/perf.txt
Evaluates the impact of adding a 'fast path' for message handling. This optimization leads to a reduction in latency for both parallel and series message operations.
```json
BEFORE: {
"openClose": 703.9341719998047,
"sendRecieve": {
"parallel": 233.59367400035262,
"series": 4531.717969999649
}
}
AFTER: { // add fast path
"openClose": 698.5278329998255,
"sendRecieve": {
"parallel": 254.588275000453,
"series": 3679.5491359978914
}
}
```
--------------------------------
### BroadcastChannel Test Logic
Source: https://github.com/pubkey/broadcast-channel/blob/master/docs/e2e.html
This section describes the logic for testing BroadcastChannel. It involves sending messages from a main context, awaiting responses from an iframe and a web worker, and repeating this process.
```javascript
// This will send a message from the main-context, await until the iframe and the web-worker answered, then repeat until all messages have been send.
// MessageCount: 0
```
--------------------------------
### LeaderElection Test Logic
Source: https://github.com/pubkey/broadcast-channel/blob/master/docs/e2e.html
This section outlines the LeaderElection test. It involves spawning multiple iframes that compete to be the leader, ensuring that only one iframe is designated as the leader at any given time.
```javascript
// This will spawn several iframes which all want to be leader. No matter what happens, exactly one iframe should be leader.
```
--------------------------------
### BroadcastChannel JavaScript Logic
Source: https://github.com/pubkey/broadcast-channel/blob/master/docs/index.html
This snippet contains the JavaScript code that implements the BroadcastChannel functionality. It handles sending and receiving messages across different browser tabs.
```javascript
import { BroadcastChannel } from 'broadcast-channel';
const channel = new BroadcastChannel('demo');
const messageInput = document.getElementById('messageInput');
const sendButton = document.getElementById('sendButton');
const messages = document.getElementById('messages');
channel.onmessage = (ev) => {
const message = document.createElement('div');
message.innerText = ev.data;
messages.appendChild(message);
};
channel.onmessageerror = (ev) => {
console.error('Received message error:', ev);
};
sendButton.onclick = () => {
const message = messageInput.value;
if (message) {
channel.postMessage(message);
messageInput.value = '';
}
};
messageInput.addEventListener('keypress', (ev) => {
if (ev.key === 'Enter') {
sendButton.click();
}
});
// Close the channel when the window is closed
window.addEventListener('unload', () => {
channel.close();
});
```
--------------------------------
### Message Send/Receive Performance (No Idle Queue)
Source: https://github.com/pubkey/broadcast-channel/blob/master/perf.txt
Compares message handling performance before and after removing the 'idle queue' mechanism. This change results in a substantial decrease in latency, particularly for series operations.
```json
BEFORE: {
"openClose": 720.8237979999976,
"sendRecieve": {
"parallel": 250.95046299998648,
"series": 3671.9275919999927
}
}
AFTER: {
"openClose": 684.5638470000122,
"sendRecieve": {
"parallel": 246.08427699981257,
"series": 2251.4478739998303
}
}
```
--------------------------------
### WebWorker Test Logic
Source: https://github.com/pubkey/broadcast-channel/blob/master/docs/e2e.html
This section details the WebWorker test. It focuses on sending messages from the main context to a worker and waiting for a response. The test runs multiple times with randomized delays to catch potential message ordering or loss issues.
```javascript
// This will send a message from the main-context to the worker and wait for a response message. This runs many times with random timings to ensure there are no edge cases where messages are missing or in the wrong order.
// MessageCount: 0
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.