- πΉ 1. Avoid Unnecessary DOM Insertions
- πΉ 2. DO NOT Rewrite the Terminal DOM
- πΉ 3. Avoid Endless Loops in Commands
- πΉ 4. Use Async/Await for Heavy Work
- πΉ 5. Limit REST Calls (Debounce & Cache)
- πΉ 6. Remove Event Listeners When Not Needed
- πΉ 7. Prevent Duplicate Script Loading
- πΉ 8. Keep Commands Simple
- πΉ 9. Avoid Printing Massive JSON Objects
- πΉ 10. Do NOT Override Core Logic in Heavy Ways
- πΉ 11. Avoid Large Inline Styles or Huge CSS Animations
- πΉ 12. Unsubscribe or Stop Running Timers
- πΉ 13. Avoid Large Audio or Image Loads
- πΉ 14. Keep Add-On Code Modular & Namespaced
- πΉ 15. Use Logging Responsibly
- π― Summary
- β Limit DOM insertions
- β Avoid infinite loops
- β Use async for heavy tasks
- β Cache REST responses
- β Avoid duplicate scripts or listeners
- β Keep commands lightweight
- β Avoid massive JSON/output
- β Avoid deep core overrides
- β Clean up timers
- β Namespace everything
- β Avoid expensive animations
How to keep custom commands, add-ons, and terminal extensions fast and stable.
The Cointacted Social Terminal is lightweight, but poorly written commands or add-ons can still cause:
- slow input
- laggy animations
- freezing output
- excessive DOM size
- duplicate event listeners
- multiple script loads
- unnecessary REST calls
This article explains how to avoid all of that.
πΉ 1. Avoid Unnecessary DOM Insertions #
Every append_line() creates a new DOM element.
β Bad:
for (let i = 0; i < 1000; i++) {
append_line("Spam line " + i);
}
This will:
- blow up DOM size
- freeze scrolling
- crash older mobile devices
β Good (batch output) #
const buffer = [];
for (let i = 0; i < 100; i++) {
buffer.push("Line " + i);
}
buffer.forEach(line =>
append_line(line)
);
Or even better β minimize output.
πΉ 2. DO NOT Rewrite the Terminal DOM #
Never do:
document.querySelector('.cointacted-social-terminal__output').innerHTML = "";
This destroys:
- history state
- scroll state
- the internal cursor
- lifecycle integrity
Use the built-in command:
clear
Or programmatically:
CointactedSocialTerminalTerminal.registry.clear(input);
πΉ 3. Avoid Endless Loops in Commands #
Dangerous:
while (true) {}
This will freeze the browser.
β Use async timers instead: #
function loop() {
// work
setTimeout(loop, 50);
}
loop();
πΉ 4. Use Async/Await for Heavy Work #
Long blocking tasks freeze the UI:
β Bad:
for (let i = 0; i < 1e9; i++) {}
append_line("Done");
β Good:
await new Promise(r => setTimeout(r));
doHeavyWorkInChunks();
πΉ 5. Limit REST Calls (Debounce & Cache) #
Example of BAD REST usage:
append_line(await fetch('/wp-json/...'));
append_line(await fetch('/wp-json/...'));
append_line(await fetch('/wp-json/...'));
This causes:
- resource spam
- rate limits
- slow responses
β Cache responses #
let cachedData = null;
CointactedSocialTerminalTerminal.registry.mycmd = async function () {
if (!cachedData) {
const res = await fetch('/wp-json/...');
cachedData = await res.json();
}
append_line(JSON.stringify(cachedData), "system");
};
πΉ 6. Remove Event Listeners When Not Needed #
Do NOT attach endless event listeners:
β Bad:
document.addEventListener('cointacted_social_terminal_output', fn);
document.addEventListener('cointacted_social_terminal_output', fn);
document.addEventListener('cointacted_social_terminal_output', fn);
If your script loads twice, you get duplicate triggers.
β Best practice: #
- Use one listener per script
- Use named functions
- Use
.removeEventListenerif needed
Example:
function onOutput(e) {
console.log(e.detail);
}
document.addEventListener('cointacted_social_terminal_output', onOutput);
// Later, if needed:
document.removeEventListener('cointacted_social_terminal_output', onOutput);
πΉ 7. Prevent Duplicate Script Loading #
Make sure your extension script loads only once:
Check:
- child themeβs functions.php
- add-on plugin
- page builders
- caching plugins injecting it multiple times
Use DevTools β Network β JS tab.
πΉ 8. Keep Commands Simple #
Commands should be:
- short
- efficient
- readable
- minimal
Avoid stuffing:
- giant data sets
- heavy processing
- multi-MB JSON
- massive logs
πΉ 9. Avoid Printing Massive JSON Objects #
Long output gives:
- slow rendering
- ugly UI
- overwhelming scroll
- lag on mobile
Instead highlight essentials:
β Bad:
append_line(JSON.stringify(bigObject), "system");
β Good:
append_line("Items: " + bigObject.items.length, "system");
append_line("Status: " + bigObject.status, "system");
πΉ 10. Do NOT Override Core Logic in Heavy Ways #
Avoid:
- overriding the run() method
- overriding parser
- overriding typing handler
- overriding deep internals
You can safely:
- wrap commands
- add events
- add your own logic
But never replace the core machinery.
πΉ 11. Avoid Large Inline Styles or Huge CSS Animations #
CSS is cheap, but:
- huge shadows
- heavy blurs
- many simultaneous animations
can slow down older devices.
Prefer clean, lightweight styles.
πΉ 12. Unsubscribe or Stop Running Timers #
If you use:
setInterval()
Make sure to clear it when done, or it will run forever.
Example:
const interval = setInterval(() => {
append_line("tick");
}, 1000);
// Stop after 5 ticks
setTimeout(() => clearInterval(interval), 5000);
πΉ 13. Avoid Large Audio or Image Loads #
Donβt load:
- large MP3 files
- uncompressed WAV
- 4K images
Keep everything lightweight.
πΉ 14. Keep Add-On Code Modular & Namespaced #
Good:
const MyAddon = {};
MyAddon.commands = {};
Bad:
let x = 10;
function a() {}
Namespace everything to avoid conflicts.
πΉ 15. Use Logging Responsibly #
Too much logging slows down DevTools:
β Bad:
console.log("message", massiveData);
β Good:
console.log("Status OK");
Or remove logs in production.
π― Summary #
To maintain blazing-fast performance:
β Limit DOM insertions #
β Avoid infinite loops #
β Use async for heavy tasks #
β Cache REST responses #
β Avoid duplicate scripts or listeners #
β Keep commands lightweight #
β Avoid massive JSON/output #
β Avoid deep core overrides #
β Clean up timers #
β Namespace everything #
β Avoid expensive animations #
Following these guidelines ensures your command ecosystem stays smooth, responsive, and future-proof.