Javascript 使用 require vs fs.readFile 读取 json 文件内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35389060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:40:06  来源:igfitidea点击:

Read json file content with require vs fs.readFile

javascriptnode.jsexpress

提问by vincentsty

Suppose that for every response from an API, i need to map the value from the response to an existing json file in my web application and display the value from the json. What are the better approach in this case to read the json file? require or fs.readfile. Note that there might be thousands of request comes in at a same time.

假设对于来自 API 的每个响应,我需要将来自响应的值映射到我的 Web 应用程序中现有的 json 文件并显示来自 json 的值。在这种情况下读取 json 文件的更好方法是什么?要求或 fs.readfile。请注意,可能有数千个请求同时传入。

Note that I do not expect there is any changes to the file during runtime.

请注意,我不希望在运行时对文件有任何更改。

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
});

回答by Shanoor

I suppose you'll JSON.parse the json file for the comparison, in that case, requireis better because it'll parse the file right away and it's sync:

我想你会 JSON.parse 用于比较的 json 文件,在这种情况下,require更好,因为它会立即解析文件并且它是同步的:

var obj = require('./myjson'); // no need to add the .json extension

If you have thousands of request using that file, require it once outside your request handler and that's it:

如果您有数千个使用该文件的请求,请在您的请求处理程序之外要求它一次,就是这样:

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
});

回答by zangw

There are two versions for fs.readFile, and they are

有两个版本fs.readFile,它们是

Asynchronous version

异步版本

require('fs').readFile('path/test.json', 'utf8', function (err, data) {
    if (err) 
       // error handling

    var obj = JSON.parse(data);
});

Synchronous version

同步版本

var json = JSON.parse(require('fs').readFileSync('path/test.json', 'utf8'));


To use requireto parse json file as below

要使用require来解析JSON文件,如下

var json = require('path/test.json');

But, note that

但是,请注意

  • requireis synchronous and only reads the file once, following calls return the result from cache

  • If your file does not have a .jsonextension, require will not treat the contents of the file as JSON.

  • require是同步的,只读取一次文件,随后的调用从缓存中返回结果

  • 如果您的文件没有.json扩展名,require 不会将文件内容视为JSON.

回答by Jehy

Since no one ever cared to write a benchmark, and I had a feeling that require works faster, I made one myself.

因为从来没有人关心写一个基准,而且我觉得需要更快地工作,所以我自己做了一个。

I compared fs.readFile (promisified version) vs require (without cache) vs fs.readFileSync.

我比较了 fs.readFile(promisified 版本)与 require(无缓存)与 fs.readFileSync。

You can see benchmark hereand results here.

您可以在此处查看基准测试并在此处查看结果。

For 1000 iterations, it looks like this:

对于 1000 次迭代,它看起来像这样:

require: 835.308ms
readFileSync: 666.151ms
readFileAsync: 1178.361ms

So what should you use? The answer is not so simple.

那么你应该使用什么?答案并非如此简单。

  1. Use require when you need to cache object forever. And better use Object.freeze to avoid mutating it in application.
  2. Use readFileSync in unit tests or on blocking application startup - it is fastest.
  3. Use readFile or promisified version when application is running and you don't wanna block event loop.
  1. 当您需要永久缓存对象时使用 require。最好使用 Object.freeze 来避免在应用程序中改变它。
  2. 在单元测试或阻止应用程序启动时使用 readFileSync - 它是最快的。
  3. 当应用程序正在运行并且您不想阻止事件循环时使用 readFile 或 promisified 版本。

回答by Stéphane Bruckert

Use node-fixturesif dealing with JSON fixtures in your tests.

如果在测试中处理 JSON 固定装置,请使用节点固定装置。

The project will look for a directory named fixtures which must be child of your test directory in order to load all the fixtures (*.js or *.json files):

该项目将查找名为 fixtures 的目录,该目录必须是您的测试目录的子目录,以便加载所有的 fixtures(*.js 或 *.json 文件):

// test/fixtures/users.json
{
  "dearwish": {
    "name": "David",
    "gender": "male"
  },
  "innaro": {
    "name": "Inna",
    "gender": "female"
  }
}
// test/users.test.js
var fx = require('node-fixtures');
fx.users.dearwish.name; // => "David" 

回答by Ajay

{
  "country": [    
    "INDIA",
    "USA"
  ],
  "codes": [   
    "IN",
    "US"
  ]
}


//countryInfo.json

const country = require('countryInfo.json').country

const code = require('countryInfo.json').code

console.log(country[0])
console.log(code[0])

回答by GoFindTruth

I only want to point out that it seems requirekeeps the file in memory even when the variables should be deleted. I had following case:

我只想指出,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));
}

The first loop with requirecan't read all JSON files because of "heap out of memory" error. The second loop with readFileworks.

require由于“堆内存不足”错误,第一个循环无法读取所有 JSON 文件。第二个循环与readFile作品。

回答by tiagolisalves

If your file is empty, require will break. It will throw an error:

如果您的文件为空,则 require 将中断。它会抛出一个错误:

SyntaxError ... Unexpected end of JSON input.

SyntaxError ... JSON 输入意外结束。

With readFileSync/readFileyou can deal with this:

随着readFileSync/readFile你可以处理这样的:

let routesJson = JSON.parse(fs.readFileSync('./routes.json', 'UTF8') || '{}');

or:

或者:

let routesJson
fs.readFile('./dueNfe_routes.json', 'UTF8', (err, data) => {
    routesJson = JSON.parse(data || '{}');
});