[JavaScript][jQuery] Checking for the existence of DOM elements

Tadashi Shigeoka ·  Tue, January 15, 2013

There are many ways to check for the existence of DOM elements in JavaScript, which can be confusing, but the conclusion is that

document.getElementById("id") != null

seems to be the fastest and best for checking.

コード速度
document.getElementById("id") != null0.019ms
$("selector")[0]0.033ms
$("selector").get(0)0.040ms
$("selector").size()0.041ms
$("selector").length0.069ms
$("selector").is("*")0.169ms

・引用元:[JS][jQuery] 要素の存在を確認する6通りのコードと実行速度 | きほんのき

jQueryによる要素の存在チェックまとめ: 小粋空間

That’s all from the Gemba.