Why is this code using so much memory?

I started doing some mock tests and even if the rate of problems in good condition is generally very low.

Could you help me? Why is this code using so much memory (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(' '));
    }
});

      

NUMBER MESSAGES PROBLEM DESCRIPTION:

Write a program that, given a sentence in which each word has a positive integer prefix and suffix, replaces the numbers while keeping the word in between. Words in a sentence are delimited by a space.

INPUT SAMPLE:

The first argument is the path to the file. Each line of the input file contains one test case, represented by a sentence. Each word in the sentence begins and ends with a one-digit integer, that is, 0 to 9. Suppose all characters are ASCII.

4Always0 5look8 4on9 7the2 4bright8 9side7 3of8 5life5
5Nobody5 7expects3 5the4 6Spanish4 9inquisition0

      

SAMPLE OUTPUT:

For each test case, print the sentence produced by replacing the numbers surrounding each word to standard output, one per line.

0Always4 8look5 9on4 2the7 8bright4 7side9 8of3 5life5
5Nobody5 3expects7 4the5 4Spanish6 0inquisition9

      

DIFFICULTIES:

The suffix and prefix of each word can be equal. Sentences are in the form of 1 to 17 words. The number of test cases is 40.

I want to try and improve my memory score, any advice is appreciated.

+3


source to share


3 answers


I would bet your problem is that you are loading the entire file into memory. Others suggest that you are reading line by line in one form or another, but that may not help. Testers can load a giant file with only one line and you will still use a lot of memory. Your best approach is to read the file in chunks. Use fs.readSync

(no need for async here). You have to process words so that you read chunks, change word numbers and continue. Sure, it's possible that they put 1 giant word in the file, but that's the next level of complexity.



+3


source


I think this will read the whole file fs.readFileSync(process.argv[2]).toString()

at once: using at least the size of the file in RAM.



Try an event-based approach .

+5


source


Instead of all this code, I suggest using regular expressions.

It doesn't matter if you have a small or huge file.

Just remove all the code and use this:

.replace(/(\d)([^\d]+)(\d)/g,'$3$2$1');

      

Example:

alert("4Always0 5look8 4on9 7the2 4bright8 9side7 3of8 5life5\n\
5Nobody5 7expects3 5the4 6Spanish4 9inquisition0".replace(/(\d)([^\d]+)(\d)/g,'$3$2$1'));
      

Run codeHide result


It's simple!

Your final code:

var fs  = require("fs");
var text = fs
    .readFileSync(process.argv[2])
    .toString() //is this really needed?
    .replace(/(\d)([^\d]+)(\d)/g,'$3$2$1');

      

This will reduce memory usage since you are only touching the string. Note that this may load the entire file into memory, which may still exhibit high usage.

+2


source







All Articles