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

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

提交回复
热议问题