為什麼 Document.html() 這麼慢? (Why is Document.html() so slow?)


問題描述

為什麼 Document.html() 這麼慢? (Why is Document.html() so slow?)

I was under the impression that the most costly method in Jsoup's API is parse().

But I just discovered that Document.html() could be even slower.

Given that the Document is the output of parse() (i.e. this is after parsing), I find this surprising.

Why is Document.html() so slow?


參考解法

方法 1:

Answering myself. The Element.html() method is implemented as:

public String html() {
  StringBuilder accum = new StringBuilder();
  html(accum); 
  return accum.toString().trim();
}

Using StringBuilder instead of String is already a good thing, and the use of StringBuilder.toString() and String.trim() may not explain the slowness of Document.html(), even for a relatively large document.

But in the middle, our method calls an overloaded version, Element.html(StringBuilder) which loops through all child nodes in the document:

private void html(StringBuilder accum) {
  for (Node node : childNodes)
    node.outerHtml(accum);
}

Thus if the document contains lots of child nodes, it will be slow.

It would be interesting to see whether there could be a faster implementation of this. 

For example, if Jsoup stores a cached version of the raw html that was provided to it via Jsoup.parse(). As an option of course, to maintain backward compatibility and small footprint in memory.

(by SouperSouper)

參考文件

  1. Why is Document.html() so slow? (CC BY-SA 3.0/4.0)

#performance #jsoup






相關問題

為什麼 Document.html() 這麼慢? (Why is Document.html() so slow?)

Sql中的WHERE,結合兩個快速條件會成倍增加成本 (WHERE in Sql, combining two fast conditions multiplies costs many times)

Tối ưu hóa truy vấn MySQL PHP - Phản hồi (MySQL PHP Query Optimization - Feedbacks)

jGRASP 上的編譯時間很慢——為什麼? (Slow compile times on jGRASP -- why?)

Pandas - 將直方圖桶分配給每一行 (Pandas - assign histogram bucket to each row)

VB.NET 中 Dictionary(Of String, SomeReferenceType) 的性能 (Performance of Dictionary(Of String, SomeReferenceType) in VB.NET)

發送到 Web 客戶端需要多少 JSON? (How much is too much JSON to send over to a web client?)

過期/緩存控制標頭的問題 (Problem with Expires/Cache-Control Headers)

iOS UI 性能分析 (iOS UI performance profiling)

限制內存機器性能的技巧?IIS 應用程序 (Tips for limit memory machine performance? IIS application)

監控 Rails 中站點性能的最佳方法是什麼 (What is the best approach to monitor site performance in rails)

決定 HTTP 標頭的字符集。我應該簡單地把 utf-8 和 fuggedaboutit 放在一起嗎? (Deciding charset for HTTP Headers. Should i simply put utf-8 and fuggedaboutit?)







留言討論