限制Javascript对象中的条目(Limiting entries in Javascript Object)

Alrighty! 我正在为我的网站开发小型聊天插件,当用户登录时,他们会看到聊天记录,我正在使用Javascript对象将所有消息存储在我的NodeJS服务器中,现在我想要它所以每当有超过50个条目在对象中添加最新消息并删除最旧的消息时,我希望这可以限制我的服务器在每次用户登录时都处理大量消息。 我将如何做到这一点?

以下是我如何存储我的消息,

var messages = { "session":[ ] }; messages.session.push( { "name":user.name, "message":safe_tags_replace(m.msg), "image":user.avatar, "user":user.steamid, "rank":user.rank, } );

我也可以在JSON对象中加载最后的五十条消息,但是每当我长时间运行我的服务器而不重新启动它时,这个对象将变得极其庞大,这是否会成为问题?

Alrighty! I'm working on small chat add-on for my website, and when a user logs on they'll see the chat history, I'm using a Javascript Object to store all messages in my NodeJS server, now I'd like it so whenever more than fifty entries are in the Object it adds the latest message and removes the oldest, I'd like this to limit my server from handling a lot of messages every time a user logs on. How would I be doing this?

Here's how I store my messages,

var messages = { "session":[ ] }; messages.session.push( { "name":user.name, "message":safe_tags_replace(m.msg), "image":user.avatar, "user":user.steamid, "rank":user.rank, } );

I could also just do loading the last fifty messages in the JSON Object but whenever I run my server for a long time without restarting it this Object will become extremly big, would this be a problem?

最满意答案

由于您将元素推送到数组的末尾,因此如果需要,可以使用数组shift()来移除数组的第一个元素。 例如

var MAX_MESSAGES = 50; if (messages.session.length > MAX_MESSAGES) { messages.session.shift(); }

回答你的问题的第二部分:

显然,您持有的数据越多,在客户端计算机上消耗的物理内存就越多。 哪些可以 - 本身 - 是一个问题,特别是对于移动设备和旧硬件。 也; 拥有巨大数组会影响查找,迭代,插入操作和排序的性能。

Since you are pushing elements to the end of your array, you could just use array shift() to remove the first element of the array if needed. e.g.

var MAX_MESSAGES = 50; if (messages.session.length > MAX_MESSAGES) { messages.session.shift(); }

To answer the second part of your question:

The more data you hold, the more physical memory you consume on the client machine, obviously. Which can - by itself - be a problem, especially for mobile devices and on old hardware. Also; having huge arrays will impact performance on lookup, iteration, some insert operations and sorting.

限制Javascript对象中的条目(Limiting entries in Javascript Object)

Alrighty! 我正在为我的网站开发小型聊天插件,当用户登录时,他们会看到聊天记录,我正在使用Javascript对象将所有消息存储在我的NodeJS服务器中,现在我想要它所以每当有超过50个条目在对象中添加最新消息并删除最旧的消息时,我希望这可以限制我的服务器在每次用户登录时都处理大量消息。 我将如何做到这一点?

以下是我如何存储我的消息,

var messages = { "session":[ ] }; messages.session.push( { "name":user.name, "message":safe_tags_replace(m.msg), "image":user.avatar, "user":user.steamid, "rank":user.rank, } );

我也可以在JSON对象中加载最后的五十条消息,但是每当我长时间运行我的服务器而不重新启动它时,这个对象将变得极其庞大,这是否会成为问题?

Alrighty! I'm working on small chat add-on for my website, and when a user logs on they'll see the chat history, I'm using a Javascript Object to store all messages in my NodeJS server, now I'd like it so whenever more than fifty entries are in the Object it adds the latest message and removes the oldest, I'd like this to limit my server from handling a lot of messages every time a user logs on. How would I be doing this?

Here's how I store my messages,

var messages = { "session":[ ] }; messages.session.push( { "name":user.name, "message":safe_tags_replace(m.msg), "image":user.avatar, "user":user.steamid, "rank":user.rank, } );

I could also just do loading the last fifty messages in the JSON Object but whenever I run my server for a long time without restarting it this Object will become extremly big, would this be a problem?

最满意答案

由于您将元素推送到数组的末尾,因此如果需要,可以使用数组shift()来移除数组的第一个元素。 例如

var MAX_MESSAGES = 50; if (messages.session.length > MAX_MESSAGES) { messages.session.shift(); }

回答你的问题的第二部分:

显然,您持有的数据越多,在客户端计算机上消耗的物理内存就越多。 哪些可以 - 本身 - 是一个问题,特别是对于移动设备和旧硬件。 也; 拥有巨大数组会影响查找,迭代,插入操作和排序的性能。

Since you are pushing elements to the end of your array, you could just use array shift() to remove the first element of the array if needed. e.g.

var MAX_MESSAGES = 50; if (messages.session.length > MAX_MESSAGES) { messages.session.shift(); }

To answer the second part of your question:

The more data you hold, the more physical memory you consume on the client machine, obviously. Which can - by itself - be a problem, especially for mobile devices and on old hardware. Also; having huge arrays will impact performance on lookup, iteration, some insert operations and sorting.