Write objects into file with Node.js

后端 未结 6 1720
滥情空心
滥情空心 2020-12-02 10:53

I\'ve searched all over stackoverflow / google for this, but can\'t seem to figure it out.

I\'m scraping social media links of a given URL page, and the function re

6条回答
  •  没有蜡笔的小新
    2020-12-02 11:39

    In my experience JSON.stringify is slightly faster than util.inspect. I had to save the result object of a DB2 query as a json file, The query returned an object of 92k rows, the conversion took very long to complete with util.inspect, so I did the following test by writing the same 1000 record object to a file with both methods.

    1. JSON.Stringify

      fs.writeFile('./data.json', JSON.stringify(obj, null, 2));
      

    Time: 3:57 (3 min 57 sec)

    Result's format:

    [
      {
        "PROB": "00001",
        "BO": "AXZ",
        "CNTRY": "649"
       },
      ...
    ]
    
    1. util.inspect

      var util = require('util');
      fs.writeFile('./data.json', util.inspect(obj, false, 2, false));
      

    Time: 4:12 (4 min 12 sec)

    Result's format:

    [ { PROB: '00001',
        BO: 'AXZ',
        CNTRY: '649' },
        ...
    ]
    

提交回复
热议问题