Is there a limit on the size of a string in JSON with Node.js?

后端 未结 4 1400
孤城傲影
孤城傲影 2020-12-03 02:46

A section of my Node.js application involves receiving a string as input from the user and storing it in a JSON file. JSON itself obviously has no limit on this, but is ther

4条回答
  •  温柔的废话
    2020-12-03 03:34

    The maximum string size in "vanilla" nodeJS (v0.10.28) is in the ballpark of 1GB.

    If your are in a hurry, you can test the maximum supported string size with a self doubling string. The system tested has 8GB of RAM, mostly unused.

    x = 'x';
    while (1){ 
         x = ''+x+x; // string context
         console.log(x.length);
    }
    
    2
    4
    8
    16
    32
    64
    128
    256
    512
    1024
    2048
    4096
    8192
    16384
    32768
    65536
    131072
    262144
    524288
    1048576
    2097152
    4194304
    8388608
    16777216
    33554432
    67108864
    134217728
    268435456
    536870912
    FATAL ERROR: JS Allocation failed - process out of memory
    Aborted (core dumped)
    

    In another test I got to 1,000,000,000 with a one char at a time for loop.

    Now a critic might say, "wait, what about JSON. the question is about JSON!" and I would shout THERE ARE NO JSON OBJECTS IN JAVASCRIPT the JS types are Object, Array, String, Number, etc.... and as JSON is a String representation this question boils down to what is the longest allowed string. But just to double check, let's add a JSON.stringify call to address the JSON conversion.

    Code

    x = 'x';
    while (1){ 
         x = ''+x+x; // string context
         console.log(JSON.stringify({a:x}).length);
    }
    

    Expectations: the size of the JSON string will start greater than 2, because the first object is going to stringify to '{"a":"xx"}' for 10 chars. It won't start to double until the x string in property a gets bigger. It will probably fail around 256M since it probably makes a second copy in stringification. Recall a stringification is independent of the original object.

    Result:

    10
    12
    16
    24
    40
    72
    136
    264
    520
    1032
    2056
    4104
    8200
    16392
    32776
    65544
    131080
    262152
    524296
    1048584
    2097160
    4194312
    8388616
    16777224
    33554440
    67108872
    134217736
    268435464
    

    Pretty much as expected....

    Now these limits are probably related to the C/C++ code that implements JS in the nodeJS project, which at this time I believe is the same V8 code used in Chrome browsers.

    There is evidence from blog posts of people recompiling nodeJS to get around memory limits in older versions. There are also a number of nodejs command line switches. I have not tested the effect of any of this.

提交回复
热议问题