小编Jan*_*ekx的帖子

使用 Node.js 写出巨大的 json (4 Gb)

我有一个非常巨大的 json 对象(使用名为 espree 的 JavaScript 解析器创建,包含一个对象数组)。我想将其写入 .json 文件,但每次都会因内存分配问题而失败(我的堆大小为 22 Gb)。

据我了解,当数据未写入文件时缓冲区会过载。

如果我仅使用同步文件操作,输出将写入文件,但应用程序的运行时间会扩展。

我尝试过但失败的解决方案(尝试序列化整个对象,然后尝试序列化数组的项目):

  • JSON.stringify
  • JSON流
  • big-json (应该将对象序列化为流,但缓冲区仍然过载..)
  • 监视耗尽事件

这是当前的代码:

const bjson= require('big-json');

function save(result) {
    let outputStream = fs.createWriteStream(/*path*/);

    const stringifyStream = bjson.createStringifyStream({
        body: result
    });

    function write(d) {
        let result = outputStream.write(d);
        if (!result) {
            outputStream.once('drain', write);
        }
    }

    stringifyStream.on('data', function (chunk) {
        writeData(chunk);
    });

    stringifyStream.on('end', function () {
        outputStream.end();
    });
}

let results = [/*results as an array, containing lots of json objects*/];
for (let i = …
Run Code Online (Sandbox Code Playgroud)

javascript json node.js

7
推荐指数
1
解决办法
2433
查看次数

ANSI C strstr() 不使用指针

我正在尝试使用 strstr() 在字符串中查找子字符串。仅使用 char[] 有效,char* 无效,导致分段错误。

所以这个正在工作:

int main() {
    char str[] = "apple apple peach";
    char *p;

    p = strstr(str, "peach");
    if (p!= NULL) {
        strncpy(p, "apple", 5);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但这一个不起作用:

int main() {
    char *str = "apple apple peach";
    char *p;

    p = strstr(str, "peach");
    if (p!= NULL) {
        strncpy(p, "apple", 5);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个既不是:

int main() {
    char *str = "apple apple peach";
    char *peach = "peach";
    char *p;

    p = strstr(str, peach); …
Run Code Online (Sandbox Code Playgroud)

c strstr strncpy string.h

0
推荐指数
1
解决办法
820
查看次数

标签 统计

c ×1

javascript ×1

json ×1

node.js ×1

string.h ×1

strncpy ×1

strstr ×1