简体   繁体   English

JavaScript中JSON.parse()的复杂性是什么?

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

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. 我将要解析一个非常大的JSON字符串,并且好奇这个内置方法的复杂性。

I would hope that it's θ(n) where n is the number of characters in the string since it can determine whether there is a syntax error or not. 我希望它是θ(n),其中n是字符串中的字符数,因为它可以确定是否存在语法错误。

I tried searching but couldn't come up with anything. 我试过搜索,但无法想出任何东西。

JSON is very simple grammar that does not require even lookaheads. JSON是非常简单的语法,甚至不需要前瞻。 As soon as GC is not involved then it is purely O(n) . 一旦GC不涉及,那么它纯粹是O(n)

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. 如果JSON主要包括字符串,那么它将是直接的并且非常线性。 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. 许多(如果不是大多数)实现将使用Java作为后端。 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. 我不希望实现使用realloc()这意味着复制数据,因此随着数组/对象变得越来越大而越来越慢。

Was curious a little more so to add more info I believe this is the "high-level" implementation of JSON.parse . 很奇怪添加更多信息我相信这是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. 这是从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. 最坏的情况可能是处理需要O(N)时间的对象的情况,其中N是字符数。
  • 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. 在传递一个reviver函数的情况下,它必须在创建它之后重新运行整个对象,但这只发生一次因此它可以忽略不计。 Also depends what the reviver function is doing and you will have to account for it's own time complexity. 还取决于reviver功能正在做什么,你将不得不考虑它自己的时间复杂性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM