为什么这段代码使用了这么多内存?

bia*_*hai 3 javascript

我开始解决一些codeeval测试,即使问题的速度得分一般很好,内存也很低.

你能帮我么?为什么这段代码使用了这么多内存(6392809 MEMORY,BYTES)?

var fs  = require("fs");
fs.readFileSync(process.argv[2]).toString().split('\n').forEach(function (line) {
    if (line != "") {
       console.log( line.split(' ').map(function(item){
            return item.substr(-1) + item.substr(1, item.length-2) + item[0];
        }).join(' '));
    }
});
Run Code Online (Sandbox Code Playgroud)

SWAP NUMBERS挑战描述:

编写一个程序,给定一个句子,其中每个单词都有一个正整数作为前缀和后缀,交换数字,同时保留其间的单词.句子中的单词之间用空格分隔.

输入样本:

第一个参数是文件的路径.输入文件的每一行包含一个由句子表示的测试用例.句子中的每个单词以单个数字正整数开始和结束,即0到9.假设所有字符都是ASCII.

4Always0 5look8 4on9 7the2 4bright8 9side7 3of8 5life5
5Nobody5 7expects3 5the4 6Spanish4 9inquisition0
Run Code Online (Sandbox Code Playgroud)

输出样本:

对于每个测试用例,打印到标准输出通过交换每个单词周围的数字获得的句子,每行一个.

0Always4 8look5 9on4 2the7 8bright4 7side9 8of3 5life5
5Nobody5 3expects7 4the5 4Spanish6 0inquisition9
Run Code Online (Sandbox Code Playgroud)

约束:

每个单词的后缀和前缀可以相等.句子长度为1到17个单词.测试用例数为40.

我想尝试提高记忆得分,欢迎任何提示.

Gho*_*ler 5

我认为这将立即在整个文件中读取:fs.readFileSync(process.argv[2]).toString()因此至少使用RAM中文件的大小.

尝试基于事件的方法.