JQuery selectors - By order of speed
source link: https://www.codesd.com/item/jquery-selectors-by-order-of-speed.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
JQuery selectors - By order of speed
I found a article regarding this a year ago but cannot find it now and all other articles I find aren't quite complete.
What I want is to build a definitive list of the quickest ways to select elements in jQuery
As Far as I understand it, If I had the following
<body>
<div id="container">
<ul class="count">
<li>One</li>
<li>Two</li>
<li class="selected">Three</li>
</ul>
</div>
</body>
In order of speed (fastest to slowest) Selecting by Id:
$('#container')
Select by Element:
$('div')
Select by class with help of element
$('ul.count')
Select by Part of Element Id with help of element (in this case ends with)
$("div[id$='tainer']")
Select by class
$('.count')
Select by Part of Element Id (in this case ends with)
$("[id$='tainer']")
Is this in the correct order of speed and have I missed any?
Thanks
performance of selectors depends on several things. browser is the main factor, with sizzle/querySelectorAll/jsengine, and jquery version which use them. Basically, jquery improves performances at each version, and does a good job to chose the best available method according to the browser.
Any kind of updates in the browser, js engine, or jquery itself can raise a new "best performer" method. Moreover, depending on the size and depth of the data a method can suddenly become faster than an other. And that's not to speak about the query itself. by example $('#id .class') might not use the same "engine" as $('#id').find('.class').
All in all, it's not a problem, jquery is quite optimized. When I stumble upon performance issues, it's quite never due to selectors (but to .append(), big , non delegated events or plugins, and overall: my plain old bad coding).
if you really, really need performance on the dom, you have to compare performance with 'document.getElementbyId' (when you have an id to use it with), and native 'document.querySelectorAll', which seems to be the fastest method of the month.
Recommend
-
152
"go-selector" is a portable implementation of kubernetes selectors.
-
126
CSS selectors in Go This package implements a CSS selector compiler for Go's HTML parsing package golang.org/x/net/html. package main import ( "fmt" "os" "strings" "github.com/ericchiang/css" "golang.org/x/n...
-
100
css-selector - The CssSelector component converts CSS selectors to XPath expressions.
-
106
Avoiding structural coupling by colocating selectors with reducers
-
53
Have you ever encountered a website that runs jQuery(location.hash) ? Seemingly pretty harmless, right? location.hash always starts with a "#" so all this code does is execute a CSS query...
-
68
Have you ever encountered a website that runs `jQuery(location.hash)`? It turns out this allows you to perform a powerful timing attack that can extract almost any secret from the HTML.
-
33
High-speed Order-Preserving Encoder (HOPE) HOPEis a fast dictionary-based compressor that encodes arbitrary byte-strings while preserving their order. It is optimized for compressing database index keys. Detaile...
-
1
JQuery .append () produced html elements that are out of order. Why? advertisements My html contains this &l...
-
4
How does the order of the combination of numpy affect the speed of multiplication? advertisements How order of numpy array influ...
-
4
jQuery 3.7.0 Released: Staying in Order jQuery 3.7.0 is now available! This release has it all: bug fixes, a new method, and a performance improvement! We even dropped our longtime selector engine: Sizzle....
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK