首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Javascript中数组的最大大小

Javascript中数组的最大大小
EN

Stack Overflow用户
提问于 2011-05-28 00:14:09
回答 5查看 156.8K关注 0票数 119

上下文:我正在构建一个小站点,它可以读取rss提要,并在后台更新/检查提要。我有一个数组用于存储要显示的数据,另一个数组用于存储已显示的记录的ID。

问:在开始变慢或变慢之前,一个数组可以在Javascript中容纳多少项。我不是对数组进行排序,而是使用jQuery的inArray函数进行比较。

网站将保持运行和更新,浏览器不太可能如此频繁地重启/刷新。

如果我应该考虑从数组中清除一些记录,那么在一个限制之后删除一些记录的最好方法是什么,比如100项。

EN

回答 5

Stack Overflow用户

发布于 2012-07-13 16:55:25

无需修剪数组,只需将其作为循环缓冲区进行寻址(索引% maxlen)。这将确保它永远不会超过限制(实现循环缓冲区意味着一旦到达末尾,您就会再次回到开头-不可能溢出数组的末尾)。

例如:

代码语言:javascript
复制
var container = new Array ();
var maxlen = 100;
var index = 0;

// 'store' 1538 items (only the last 'maxlen' items are kept)
for (var i=0; i<1538; i++) {
   container [index++ % maxlen] = "storing" + i;
}

// get element at index 11 (you want the 11th item in the array)
eleventh = container [(index + 11) % maxlen];

// get element at index 11 (you want the 11th item in the array)
thirtyfifth = container [(index + 35) % maxlen];

// print out all 100 elements that we have left in the array, note
// that it doesn't matter if we address past 100 - circular buffer
// so we'll simply get back to the beginning if we do that.
for (i=0; i<200; i++) {
   document.write (container[(index + i) % maxlen] + "<br>\n");
}
票数 28
EN

Stack Overflow用户

发布于 2011-05-28 00:21:50

您可以尝试如下所示来测试和修剪长度:

http://jsfiddle.net/orolo/wJDXL/

代码语言:javascript
复制
var longArray = [1, 2, 3, 4, 5, 6, 7, 8];

if (longArray.length >= 6) {
  longArray.length = 3;
}

alert(longArray); //1, 2, 3

票数 6
EN

Stack Overflow用户

发布于 2011-05-28 01:07:40

我已经构建了一个性能框架,可以操作数百万个数据集并绘制图形,即使这样,javascript的计算延迟也只有几十毫秒。除非您担心超过数组大小限制,否则我认为您没有太多需要担心的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6154989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档