Javascript 使用require vs fs.readFile读取json文件内容
假设对于来自API的每个响应,我需要将响应中的值映射到web应用程序中现有的json文件,并显示json文件中的值。在这种情况下,读取json文件的更好方法是什么?需要或fs.readfile。请注意,可能有数千个请求同时传入 请注意,我不希望在运行时对文件进行任何更改Javascript 使用require vs fs.readFile读取json文件内容,javascript,node.js,express,Javascript,Node.js,Express,假设对于来自API的每个响应,我需要将响应中的值映射到web应用程序中现有的json文件,并显示json文件中的值。在这种情况下,读取json文件的更好方法是什么?需要或fs.readfile。请注意,可能有数千个请求同时传入 请注意,我不希望在运行时对文件进行任何更改 request(options, function(error, response, body) { // compare response identifier value with json file in node
request(options, function(error, response, body) {
// compare response identifier value with json file in node
// if identifier value exist in the json file
// return the corresponding value in json file instead
});
我想您将使用JSON.parse来比较JSON文件,在这种情况下,
require
更好,因为它将立即解析文件,并且是同步的:
var obj = require('./myjson'); // no need to add the .json extension
如果有数千个请求使用该文件,请在请求处理程序外部请求一次,即:
var myObj = require('./myjson');
request(options, function(error, response, body) {
// myObj is accessible here and is a nice JavaScript object
var value = myObj.someValue;
// compare response identifier value with json file in node
// if identifier value exist in the json file
// return the corresponding value in json file instead
});
fs.readFile
有两个版本,它们是
异步版本
require('fs').readFile('path/test.json', 'utf8', function (err, data) {
if (err)
// error handling
var obj = JSON.parse(data);
});
var json = JSON.parse(require('fs').readFileSync('path/test.json', 'utf8'));
同步版本
require('fs').readFile('path/test.json', 'utf8', function (err, data) {
if (err)
// error handling
var obj = JSON.parse(data);
});
var json = JSON.parse(require('fs').readFileSync('path/test.json', 'utf8'));
要使用
require
解析json文件,如下所示
var json = require('path/test.json');
但是,请注意
是同步的,只读取文件一次,后续调用从缓存返回结果require
- 如果文件没有扩展名
,require将不会将文件内容视为.json
json
我只想指出,
require
似乎将文件保存在内存中,即使在应该删除变量时也是如此。我有以下情况:
for (const file of fs.readdirSync('dir/contains/jsons')) {
// this variable should be deleted after each loop
// but actually not, perhaps because of "require"
// it leads to "heap out of memory" error
const json = require('dir/contains/jsons/' + file);
}
for (const file of fs.readdirSync('dir/contains/jsons')) {
// this one with "readFileSync" works well
const json = JSON.parse(fs.readFileSync('dir/contains/jsons/' + file));
}
由于“内存不足”错误,
require
的第一个循环无法读取所有JSON文件。第二个循环使用readFile
如果文件为空,require将中断。它将抛出一个错误:
合成错误。。。JSON输入意外结束
使用readFileSync/readFile
可以处理以下问题:
let routesJson = JSON.parse(fs.readFileSync('./routes.json', 'UTF8') || '{}');
或:
因为从来没有人愿意写基准测试,而且我觉得需要更快的工作,所以我自己做了一个 我比较了fs.readFile(promisified版本)与require(无缓存)与fs.readFileSync 您可以看到基准测试和结果 对于1000次迭代,它如下所示:
require: 835.308ms
readFileSync: 666.151ms
readFileAsync: 1178.361ms
那么你应该用什么呢?答案并不那么简单
我想知道为什么我的require('file.json')一直在更改,直到我读到这篇文章——“require是同步的,只读取文件一次,下面的调用从缓存返回结果”。这可以通过-delete require.cache[require.resolve('file.json')]来绕过,它不是
require
synchronous吗?根据JSON文件或用例的大小,fs.readFile应该更好。我也同意,甚至建议对较大的文件使用管道。对于大型连接,连接的鸽子洞(空间/时间索引)也可以提高效率。值得注意的是,require
将缓存文件,因此如果该json文件的内容在应用程序的整个生命周期内发生变化,您将看不到可以执行的对象分解更新const{country,code}=require('countryInfo.json')代码>
let routesJson
fs.readFile('./dueNfe_routes.json', 'UTF8', (err, data) => {
routesJson = JSON.parse(data || '{}');
});
require: 835.308ms
readFileSync: 666.151ms
readFileAsync: 1178.361ms