如何加快获取javascript数组数据的速度
我想获取20000个文件的创建日期并将其存储在数组中。如何加快获取javascript数组数据的速度,javascript,node.js,node-red,Javascript,Node.js,Node Red,我想获取20000个文件的创建日期并将其存储在数组中。 总完成时间为35分钟,相当长。() 有没有办法创建处理时间更快的阵列 获取如下文件创建日期数组的当前逻辑是否有问题? ① 数组声明:var arr=[] ② 我使用以下代码获取文件创建日期: var fs = global.get('fs'); // Get file creation date time msg.createdDate = fs.statSync(msg.pathFName).birthtime; return msg;
总完成时间为35分钟,相当长。()
有没有办法创建处理时间更快的阵列 获取如下文件创建日期数组的当前逻辑是否有问题?
① 数组声明:
var arr=[]代码>
② 我使用以下代码获取文件创建日期:
var fs = global.get('fs');
// Get file creation date time
msg.createdDate = fs.statSync(msg.pathFName).birthtime;
return msg;
③ 在数组中存储创建日期。
我的流程:
[
{
“id”:“de157360.11d49”,
“类型”:“选项卡”,
“标签”:”フロー 1",
“残疾”:假,
“信息”:”
},
{
“id”:“6a8db36.f55e14c”,
“类型”:“注入”,
“z”:“de157360.11d49”,
“名称”:“,
“道具”:[
{
“p”:“有效载荷”
},
{
“p”:“主题”,
“vt”:“str”
}
],
“重复”:“,
“crontab”:“,
“一次”:错,
“onceDelay”:0.1,
“主题”:“,
“有效载荷”:“,
“payloadType”:“str”,
“x”:750,
“y”:160,
“电线”:[
[
“4907d69b.07f738”
]
]
},
{
“id”:“4907d69b.07f738”,
“类型”:“更改”,
“z”:“de157360.11d49”,
“名称”:“,
“规则”:[
{
“t”:“集合”,
“p”:“路径文件”,
“pt”:“msg”,
“收件人”:“D:\\Data\\”,
“tot”:“str”
}
],
“行动”:“,
“财产”:“,
“从”:“从”,
“致”:“,
“reg”:错误,
“x”:910,
“y”:160,
“电线”:[
[
“d68da128.cd6d9”
]
]
},
{
“id”:“d68da128.cd6d9”,
“类型”:“fs ops dir”,
“z”:“de157360.11d49”,
“名称”:“,
“路径”:“路径文件”,
“路径类型”:“msg”,
“过滤器”:“*”,
“filterType”:“str”,
“目录”:“文件”,
“dirType”:“msg”,
“x”:1080,
“y”:160,
“电线”:[
[
“764296f2.688338”
]
]
},
{
“id”:“764296f2.688338”,
“类型”:“更改”,
“z”:“de157360.11d49”,
“名称”:“f=0”,
“规则”:[
{
“t”:“集合”,
“p”:“f”,
“pt”:“msg”,
“收件人”:“0”,
“tot”:“num”
},
{
“t”:“集合”,
“p”:“arrObjFiles”,
“pt”:“msg”,
“致”:“[]”,
“tot”:“json”
}
],
“行动”:“,
“财产”:“,
“从”:“从”,
“致”:“,
“reg”:错误,
“x”:1210,
“y”:160,
“电线”:[
[
“ab664988.c25da8”,
“232fdda0.b11e22”
]
]
},
{
“id”:“1912092c.fbab77”,
“类型”:“更改”,
“z”:“de157360.11d49”,
“名称”:“f++”,
“规则”:[
{
“t”:“集合”,
“p”:“f”,
“pt”:“msg”,
“收件人”:“$.f+1\t”,
“tot”:“jsonata”
}
],
“行动”:“,
“财产”:“,
“从”:“从”,
“致”:“,
“reg”:错误,
“x”:1650,
“y”:60,
“电线”:[
[
“ab664988.c25da8”
]
]
},
{
“id”:“ab664988.c25da8”,
“类型”:“开关”,
“z”:“de157360.11d49”,
“name”:“u您使用的是fs.statSync
,这是一个同步函数,意味着每次调用它时,所有代码执行都会停止,直到该函数完成。请研究使用fs.stat
(异步版本),映射到文件路径数组,并使用
使用fs.stat
(异步版本)函数,您可以一次启动多个文件的调用,这样总体上会更快(因为可以一次加载多个文件,而不必等待超慢的文件)
以下是我的意思示例,您可以在浏览器中运行:
const arr=[“你好”、“我”、“是”、“在这里”];
//这是一个返回承诺的函数示例。
//此函数返回一个承诺,该承诺在'seconds'秒数之后解析,并将'word'的值返回为大写
const sleep和大写=(秒,字)=>新承诺(
(解析)=>设置超时(
()=>解析(word.toUpperCase()),秒*1000
)
);
//这包罗了一系列承诺
我保证(
//这会将单词数组转换为上面定义的sleepAndUppercase函数生成的承诺数组
arr.map((word,i)=>sleep和大写(i,word))
//下一行等待所有promies完成,并将结果用作变量listOfWords,
//这是一个大写的单词数组
).然后((listOfWords)=>{
//我们可以记录结果
console.log(listOfWords);
//我们可以根据这些结果创建变量
const语句=listOfWords.join(“”);
控制台日志(句子);
});
console.log(“Promissions started”)
感谢您的热情指导,我将尝试使用fs.stat
[
{
"id": "de157360.11d49",
"type": "tab",
"label": "フロー 1",
"disabled": false,
"info": ""
},
{
"id": "6a8db36.f55e14c",
"type": "inject",
"z": "de157360.11d49",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "str",
"x": 750,
"y": 160,
"wires": [
[
"4907d69b.07f738"
]
]
},
{
"id": "4907d69b.07f738",
"type": "change",
"z": "de157360.11d49",
"name": "",
"rules": [
{
"t": "set",
"p": "pathFiles",
"pt": "msg",
"to": "D:\\Data\\",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 910,
"y": 160,
"wires": [
[
"d68da128.cd6d9"
]
]
},
{
"id": "d68da128.cd6d9",
"type": "fs-ops-dir",
"z": "de157360.11d49",
"name": "",
"path": "pathFiles",
"pathType": "msg",
"filter": "*",
"filterType": "str",
"dir": "files",
"dirType": "msg",
"x": 1080,
"y": 160,
"wires": [
[
"764296f2.688338"
]
]
},
{
"id": "764296f2.688338",
"type": "change",
"z": "de157360.11d49",
"name": "f=0",
"rules": [
{
"t": "set",
"p": "f",
"pt": "msg",
"to": "0",
"tot": "num"
},
{
"t": "set",
"p": "arrObjFiles",
"pt": "msg",
"to": "[]",
"tot": "json"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1210,
"y": 160,
"wires": [
[
"ab664988.c25da8",
"232fdda0.b11e22"
]
]
},
{
"id": "1912092c.fbab77",
"type": "change",
"z": "de157360.11d49",
"name": "f++",
"rules": [
{
"t": "set",
"p": "f",
"pt": "msg",
"to": "$.f + 1\t",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1650,
"y": 60,
"wires": [
[
"ab664988.c25da8"
]
]
},
{
"id": "ab664988.c25da8",
"type": "switch",
"z": "de157360.11d49",
"name": "u<number of files",
"property": "f",
"propertyType": "msg",
"rules": [
{
"t": "lt",
"v": "files.length",
"vt": "msg"
},
{
"t": "else"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 1410,
"y": 160,
"wires": [
[
"15154524.a6828b"
],
[
"c7625457.8ccb18"
]
]
},
{
"id": "f8030294.f6334",
"type": "function",
"z": "de157360.11d49",
"name": "Get file creation date time",
"func": "var fs = global.get('fs');\n// Get file creation date time\nmsg.createdDate = fs.statSync(msg.pathFName).birthtime;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 1830,
"y": 160,
"wires": [
[
"59879c16.6c7564"
]
]
},
{
"id": "59879c16.6c7564",
"type": "change",
"z": "de157360.11d49",
"name": "Storage file creation date time",
"rules": [
{
"t": "set",
"p": "objFiles",
"pt": "msg",
"to": "{}",
"tot": "json"
},
{
"t": "set",
"p": "objFiles",
"pt": "msg",
"to": "$merge([$.objFiles,{'fileName': $.fileName,'createdDate': $.createdDate}])",
"tot": "jsonata"
},
{
"t": "set",
"p": "arrObjFiles",
"pt": "msg",
"to": "$append(arrObjFiles, [objFiles])",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 2090,
"y": 160,
"wires": [
[
"1912092c.fbab77"
]
]
},
{
"id": "15154524.a6828b",
"type": "change",
"z": "de157360.11d49",
"name": "",
"rules": [
{
"t": "set",
"p": "fileName",
"pt": "msg",
"to": "$.files[$$.f]",
"tot": "jsonata"
},
{
"t": "set",
"p": "pathFName",
"pt": "msg",
"to": "pathFiles & fileName",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1620,
"y": 160,
"wires": [
[
"f8030294.f6334"
]
]
},
{
"id": "c7625457.8ccb18",
"type": "debug",
"z": "de157360.11d49",
"name": "END",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "arrObjFiles",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1590,
"y": 220,
"wires": []
},
{
"id": "232fdda0.b11e22",
"type": "debug",
"z": "de157360.11d49",
"name": "START",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "files.length",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1380,
"y": 220,
"wires": []
}
]