What is the complexity of JSON.parse() in JavaScript?

前端 未结 3 1247
醉酒成梦
醉酒成梦 2021-02-08 22:55

The title says it all. I\'m going to be parsing a very large JSON string and was curious what the complexity of this built in method was.

I would hope that it\'s θ(n) w

相关标签:
3条回答
  • 2021-02-08 22:58

    I do not know of the implementations in browsers, but your assumption is correct to a certain point. If the JSON includes mainly strings, it will be straight forward and very linear. If you have many floating points, it will take a bit of time to convert the numbers, but again quite linear (numbers with more digits take slightly longer, but in comparison to a long string... very similar).

    Since in most cases arrays and objects are declared as maps, the memory allocation grows as required and will generally be linear. Many (if not most) implementations will make use of Java as a backend. This means garbage collection and thus a quite impossible way to know for sure how much time will be required to transform all the data as it will very much depend on things such as the size of the memory model used on the target computer and how often the garbage collection runs. However, it should generally just grow as items are added to the map and it will thus mostly look like it is linear as well. I would not expect an implementation to make use of a realloc() which would mean copying data and thus being slower and slower as an array/object grows bigger and bigger.

    0 讨论(0)
  • 2021-02-08 23:02

    Was curious a little more so to add more info I believe this is the "high-level" implementation of JSON.parse. I tried finding if Chromium has their own source for it and not sure if this is it? This is going off of the source from Github.

    Things to note:

    • worst case is probably the scenario of handling objects which requires O(N) time where N is the number of characters.
    • in the case a reviver function is passed, it has to rewalk the entire object after it's created but that only happens once so it's fairly negligible. Also depends what the reviver function is doing and you will have to account for it's own time complexity.
    0 讨论(0)
  • 2021-02-08 23:04

    JSON is very simple grammar that does not require even lookaheads. As soon as GC is not involved then it is purely O(n).

    0 讨论(0)
提交回复
热议问题