Holisticz - Embrace the Infinite

Step into the world of endless opportunities and unlock the gateway to virtual transactions.

  • Domain Update: Our Forum has moved! Please visit us at Holisticz.com for the latest updates.

3 lines of code that could significantly improve performance

0

0%

Status

Offline

Posts

3

Likes

0

Rep

0

Bits

0

2

Years of Service

I noticed that after leaving my computer on the home page for a while, the performance of the page deteriorated significantly.

I debugged it a bit and found that shoutbox messages are never removed from the browser. This is problematic because every addition of a message causes the rest of the messages to be reindexed. The more messages there are, the more processing power is needed to add a new one.

My suggestion is to add a limit of messages available on the shoutbox (In my example I did 20, but it could be any arbitrary number of your choice), after which, old messages get automatically removed from the client's browser.

Here's the code in question:

Code:
Code:
const container = e('#container-' + t.room);
if(container.childElementCount > 20)
container.removeChild(container.children[container.childElementCount-1]);

This content would be introduced in the following position of the client side source code:

Code:
Code:
... 
handleEvents: function () {
this.socket.on(2, t=>{
void 0 !== t.message.mode && (this.mode = t.message.mode),
e('#container-' + t.room).prepend(this.buildTemplate(t.message))
}),
...

Making it look like this:
Code:
Code:
... 
handleEvents: function () {
this.socket.on(2, t=>{
const container = e('#container-' + t.room);
void 0 !== t.message.mode && (this.mode = t.message.mode),
container.prepend(this.buildTemplate(t.message));
if(container.childElementCount > 20)
container.removeChild(container.children[container.childElementCount-1]);
}),
...
 

Mastiff

Member

0

0%

Status

Offline

Posts

23

Likes

8

Rep

15

Bits

0

1

Years of Service

48,610

38,214

238,323

Top