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

前端 未结 3 1246
醉酒成梦
醉酒成梦 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.

提交回复
热议问题