- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是 node.js 的新手。
我尝试将 70000 项插入数组,然后将其全部删除:
var Stopwatch = require("node-stopwatch").Stopwatch;
var stopwatch = Stopwatch.create();
var a = []
stopwatch.start();
for (var i = 1 ; i < 70000 ; i++){
a.push((parseInt(Math.random() * 10000)) + "test");
}
for (var i = 1 ; i < 70000 ; i++){
a.splice(0,1);
}
stopwatch.stop();
console.log("End: " + stopwatch.elapsedMilliseconds + " : " + a.length);
它工作正常,输出是:
PS C:\Users\Documents\VSCode> node test.js
End: 51 : 0
但是当我将项目数量增加到 72000 时,结束需要太多时间:
var Stopwatch = require("node-stopwatch").Stopwatch;
var stopwatch = Stopwatch.create();
var a = []
stopwatch.start();
for (var i = 1 ; i < 72000 ; i++){
a.push((parseInt(Math.random() * 10000)) + "test");
}
for (var i = 1 ; i < 72000 ; i++){
a.splice(0,1);
}
stopwatch.stop();
console.log("End: " + stopwatch.elapsedMilliseconds + " : " + a.length);
输出是:
End: 9554 : 0
为什么会发生?才加了2000多条,但是太费时间了。
Node.js 版本是:v6.11.3
最佳答案
这里是 V8 开发人员。在开头(在 array[0]
处)删除(或插入)数组元素通常非常昂贵,因为必须移动所有剩余元素。本质上,引擎必须在引擎盖下为这些中的每一个做些什么 .splice(0, 1)
操作是:
for (var j = 0; j < a.length - 1; j++) {
a[j] = a[j+1];
}
a.length = a.length - 1`
在某些情况下,V8 可以在引擎盖下使用一个技巧,即移动对象的开头——在快速的情况下,您可以看到这个技巧提供的惊人加速。但是,由于技术原因,这个技巧不能应用于超过一定大小的数组。由此产生的“减速”实际上是这种非常昂贵的操作的“真实”速度。
如果要快速删除数组元素,请从末尾(在 array[array.length - 1]
处)删除它们,例如使用 Array.pop()
.如果想一次性删除所有元素,设置array.length = 0
即可。 .如果您需要快速 FIFO/“队列”语义,请考虑从环形缓冲区中汲取灵感:为要读取/返回的下一个元素设置一个“游标”,并且仅在有大量元素要释放时才收缩数组。大致:
function Queue() {
this.enqueue = function(x) {
this.array_.push(x);
}
this.dequeue = function() {
var x = this.array_[this.cursor_++];
// Free up space if half the array is unused.
if (this.cursor_ > this.array_.length / 2) {
this.array_.splice(0, this.cursor_);
this.cursor_ = 0;
}
return x;
}
this.array_ = [];
this.cursor_ = 0;
}
旁注:这里无关紧要,但为了记录,要将 70,000 个元素插入数组,循环应从 0: for (var i = 0; i < 70000; i++) {...}
开始。 .如所写,您仅推送 69,999 个元素。
旁注 2:通过“parseInt”将 double 舍入为整数非常慢,因为它首先将 double 格式化为字符串,然后将该字符串读回为整数。更快的方法是 Math.floor(Math.random() * 10000))
. (出于本次测试的目的,您也可以简单地按 i
。)
关于arrays - node.js 拼接超过 70000 个项目时速度太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46451343/
我使用这个 cmd 应用程序 https://github.com/tokland/youtube-upload 上传 50 个视频后,我收到此错误: [RequestError] Server re
尝试将 docker 容器提交到镜像时出现错误: [root@dockerregistry /]# docker commit da4180ab1536 dockerregistry:5000/myi
我只是想知道这样做会更好吗: if((fd = open(filename, O_RDWR)) == -1) { fprintf(stderr, "open [ %s ]\n", strerror(e
我在我的开发机器(单个笔记本)中使用 Elasticsearch 1.4.4。一切都设置为默认值,因为我从未更改过任何设置。 当我启动它时,我通常会收到以下消息: [2015-10-27 09:38:
我收到错误 Lock wait timeout exceeded;尝试重新启动事务。这是什么原因,如何解决?仅供引用:MySQL 配置文件中的 innodb_lock_wait_timeout = 1
我对 Slack 中的 block 功能有疑问。有人设法构建 3 列而不是 2 列吗? 我凭直觉尝试了以下代码,但它不起作用: { "blocks": [ {
div 中的内容超过由 css 决定的固定大小。 #fixeddiv { position: fixed; margin: auto; max-height: 300px
我想将 EditText 字段限制为 150 个字符,我可以很容易地做到这一点。但是,当用户试图超过该限制时,我还需要通过键入(即第 151 个字符)或粘贴超过 150 个字符来提醒用户。 解决这个问
我目前正在使用此代码并排记录两个窗口: ffmpeg -f gdigrab -framerate 30 -i title="" -f gdigrab -framerate 30 -i title=""
我在几个包含长字符串的单元格上使用嵌套的 SUBSTITUE 函数,并定期更新 SUBSTITUE fx,这导致我将其复制并粘贴到所有需要它的单元格中。问题是,我的 SUBSTITUTE 列表会随着时
我创建了一个标题 div,如下所示:
Here is the demo link 您怎么看,页面中只有 8 个广告可见,但我有 14 个广告。我已阅读有关广告限制的信息 here但不明白是不是我的情况?有人可以给我确切的答案,为什么我看不
我的非常简单的算法 - C 中的快速排序有问题。它非常有效(随机化大约 0.1 秒并检查列表是否已排序)但是当我想要对超过 500k 的元素进行排序时它会崩溃。不幸的是,我需要对它们进行更多排序,因为
我成功解决了一个关于 Hackerrank 的问题,它通过了所有测试用例,但我得到了一个错误,超过了时间限制。我猜如果我优化我的代码它会工作,但我想不出任何方法来使我的代码更有效率。 问题是: 对大小
你会如何用 包围下面的文字3 个反引号 ```使用 tpope 的 Vim Surround . 我所能做的就是 1 个反引号 使用 S`在视觉模式下: 最佳答案 这不是你问的,但这可以在没有环绕的情
我目前有一个模拟账户。我正在尝试为我的雇主使用 SwifType 制作 POC。我们有一个非常大的数据库,每 1 小时索引一次,并创建一个 JSON 文件。我认为与 Elastic 的集成将非常容易,
我为一个大约有 100 到 120 名成员的小型组织维护网站。 每个月,我们都会发送一封电子邮件,通知我们的成员我们将在即将举行的 session 中讨论的主题。 我正在尝试使用我们的网站为我们提供一
这个问题已经有答案了: How to automatically input an array formula as string with more than 255 characters in l
我有一个在 JBoss 6.1 中运行的 JSF 应用程序,它使用内部Tomcat Servlet 容器。 我已经通过apache commons文件上传实现了上传。我想防止上传过大的文件并设置了属性
当我尝试在 PyMySQL 上执行查询时,出现以下错误: pymysql.err.InternalError: (1205, 'Lock wait timeout exceeded; try rest
我是一名优秀的程序员,十分优秀!