SlideShare une entreprise Scribd logo
1  sur  41
Télécharger pour lire hors ligne
Selectors
Santiago Aimetta
Types sorted by performance
● Types sorted by performance / souders
1. ID, i.e. #header
2. Class, i.e. .promo
3. Type, i.e. div
4. Adjacent sibling, i.e. h2 + p
5. Child, i.e. div > p
6. Descendant, i.e. ul a
7. Universal, i.e. *
8. Attribute, i.e. [type="text"]
9. Pseudo-classes/-elements, i.e. a:hover
Performance impact
● "The impact of CSS selectors on
performance derives from the amount of
time it takes the browser to match the
selector against the elements in the
document" Souders
How selectors works
● Browsers read selectors right to left
● Example: #div-1 a
○ We read #div-1 with an a
○ Browser reads a in #div-1
● Why?
Example reading left to right
document
html
head body
div id:div-1 div id:div-2
a class: nav-
link
a class: nav-
link
span
a
● div a {... }
● div > span .classX
● div img {...}
● div a img {...}
● #div-1 a {...}
● #div-2 {...}
● #div-1 a span {...}
● div span a {...}
● .class-x a {...}
● .class-z {...}
● div a span {...}
● span a {...}
● div > a {...}
● a.nav-link + a {...}img span
Example reading left to right
document
html
head body
div id:div-1 div id:div-2
a class: nav-
link
a class: nav-
link
span
a
● div a {... }
● div > span .classX {..}
● div img {...}
● div a img {...}
● #div-1 a {...}
● #div-2 {...}
● #div-1 a span {...}
● div span a {...}
● .class-x a {...}
● .class-z {...}
● div a span {...}
● span a {...}
● div > a {...}
● a.nav-link + a {...}img span
Example reading left to right
document
html
head body
div id:div-1 div id:div-2
a class: nav-
link
a class: nav-
link
span
a
● div a {... }
● div > span .classX {..}
● div img {...}
● div a img {...}
● #div-1 a {...}
● #div-2 {...}
● #div-1 a span {...}
● div span a {...}
● .class-x a {...}
● .class-z {...}
● div a span {...}
● span a {...}
● div > a {...}
● a.nav-link + a {...}img span
Example reading right to left
document
html
head body
div id:div-1 div id:div-2
a class: nav-
link
a class: nav-
link
span
a
● div a {... }
● div > span .classX{..}
● div img {...}
● div a img {...}
● #div-1 a {...}
● #div-2 {...}
● #div-1 a span {...}
● div span a {...}
● .class-x a {...}
● .class-z {...}
● div a span {...}
● span a {...}
● div > a {...}
● a.nav-link + a {...}img span
Example reading right to left
document
html
head body
div id:div-1 div id:div-2
a class: nav-
link
a class: nav-
link
span
a
● div a {... }
● div > span .classX {..}
● div img {...}
● div a img {...}
● #div-1 a {...}
● #div-2 {...}
● #div-1 a span {...}
● div span a {...}
● .class-x a {...}
● .class-z {...}
● div a span {...}
● span a {...}
● div > a {...}
● a.nav-link + a {...}img span
Example reading right to left
document
html
head body
div id:div-1 div id:div-2
a class: nav-
link
a class: nav-
link
span
a
● div a {... }
● div {...}
● div img {...}
● div a img {...}
● #div-1 a {...}
● #div-2 {...}
● #div-1 a span {...}
● div span a {...}
● .class-x a {...}
● .class-z {...}
● div a span {...}
● span a {...}
● div > a {...}
● a.nav-link + a {...}img span
How selectors works
● When the browser is trying to style an
element has to discard style rules fast
● Starting for the rightmost part of the selector
discards a lot of rules at once
● Keep in mind that a common page has
hundred of rules
● The rightmost part of the selector is called
key selector
● The key selector has a significant impact
on the performance of CSS selectors
Key Selector
● Rightmost part of the selector
● #some-id .some-class {...}
● div .some-class a {...}
● .some-class * {...}
● The matching work that the browser do is
heavily affected by the rightmost selector
Tips to efficient selectors
● Avoid universal rules *:
○ Try to avoid universal selectors like *, adjacent
sibling, child, descendant and attribute. The
recomended selectors are id and class.
● Don't qualify id selectors:
○ There is only one element per id, so there is no need
to add additional qualifiers.
● Don't qualify class selectors:
○ Instead of qualifying class selectors for tags, create
a new class, ie: a.nav-link use .a-nav-link .
● Specific rules
Tips to efficient selectors
● Avoid descendant selectors:
○ They are one of the most expensive to process
● Avoid tag child selectors:
○ tag child selectors like .content > p > a can be
replaced by a specific class like .content-anchor
● Use the inheritance:
○ Use the inherited properties, dont repeat.
● The number of rules and the dom deep has
an impact in performance
Focus on selectors where the key selector
matches a large number of elements in the
page.
... Too much tips, i lost the focus
Tunning selectors
● Example
<ul id="nav-links">
<li><a href="http://login.mercadolibre.com.ar">Ingresar</a> </li>
<li><a href="http://myaccount.mercadolibre.com.ar/">Mi cuenta</a></li>
<li><a href="https://syi.mercadolibre.com.ar/sell">Vender</a></li>
<li><a href="http://www.mercadolibre.com.ar/ayuda_home">Ayuda</a></li>
</ul>
● #nav-links a {...} will evaluate all a elements
and then check if it belongs to an element
with nav-links id
Tunning selectors
● Example
<ul id="nav-links">
<li><a class="nav-link" href="http://login.mercadolibre.com.ar">Ingresar</a>
</li>
<li><a class="nav-link" href="http://myaccount.mercadolibre.com.ar/">Mi
cuenta</a></li>
<li><a class="nav-link" href="https://syi.mercadolibre.com.ar/sell"
>Vender</a></li>
<li><a class="nav-link" href="http://www.mercadolibre.com.ar/ayuda_home"
>Ayuda</a></li>
</ul>
● .nav-link {...} this will evaluate only the
elements with the nav-link class, that are
fewer, producing a fast search
Side impact
● Selectors not only affect the load time.
● The selectors has an impact on reflows
● Reflows are triggered when DOM element's
style properties are modified by javascript.
● Reflows require that the browser re apply the
rules, which means match all CSS selectors
once again
● Inefficient selectors -> slow reflows ->
sluggish page
Tradeoff
● Generate more classes and ids to avoid
universal selectors add weight to the page.
● More css rules.
● More class and id attributes.
● Less flexibility of the styles.
● Css selector replacing / rewriting is
expensive in time and effort.
Focus on selectors where the key selector
matches a large number of elements in the
page.
once again!
Selectors with Javascript and
JQuery
● Different types, kind of convinations ->
Different performance
Selectors JQuery
● The best: $("#someId")
because the native use of document.
getElementById()
● Slow: $(".someClassName")
because document.getElementsByClassName() is
not supported in all browsers IE5-8
● The worst: $(":pseudo") or $("[attribute=value]")
because there is no native calls.
in modern browsers querySelector()
querySelectorAll() helps
Selectors JQuery
● The best: $("#someId")
because the native use of document.
getElementById()
● Slow: $(".someClassName")
because document.getElementsByClassName() is
not supported in all browsers IE5-8
● The worst: $(":pseudo") or $("[attribute=value]")
because there is no native calls.
in modern browsers querySelector()
querySelectorAll() helps
Selectors JQuery
● The best: $("#someId")
because the native use of document.
getElementById()
● Slow: $(".someClassName")
because document.getElementsByClassName() is
not supported in all browsers IE5-8
● The worst: $(":pseudo") or $("[attribute=value]")
because there is no native calls.
in modern browsers querySelector()
querySelectorAll() helps
● http://jsperf.com/santi-selectors-test/4
Id selector
Class selector
Type selector
Attribute selector
Pseudo selector
Jquery object
JSPerf - Test Case Details
JSPerf - Preparation Code
JSPerf - Setup & teardown
JSPerf - Code snippets
JSPerf - Command buttons
JSPerf - Test Runner
JSPerf - Results
JSPerf - Charts
Links
● http://stackoverflow.com/questions/5797014/why-do-browsers-match-css-selectors-
from-right-to-left
● http://csswizardry.com/2011/09/writing-efficient-css-selectors/
● http://www.stevesouders.com/blog/2009/03/10/performance-impact-of-css-selectors/
● http://www.stevesouders.com/blog/2009/06/18/simplifying-css-selectors/
● http://answers.oreilly.com/topic/647-how-to-write-efficient-css-selectors/
● https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Writing_efficient_CSS?
redirectlocale=en-US&redirectslug=CSS%2FWriting_Efficient_CSS
● http://www.html5rocks.com/en/tutorials/internals/howbrowserswork
● http://reference.sitepoint.com/css/cascade
● http://caniuse.com/getelementsbyclassname
● http://caniuse.com/queryselector
● Picture: http://www.flickr.com/photos/29008702@N06/5137285917/

Contenu connexe

Similaire à Selectors Performance

Similaire à Selectors Performance (20)

#4 - CSS Selectors, CSS3 and Web typography
#4 - CSS Selectors, CSS3 and Web typography#4 - CSS Selectors, CSS3 and Web typography
#4 - CSS Selectors, CSS3 and Web typography
 
Solution Challenge GDSC GHRCE WEB DEVELOPMENT.pdf
Solution Challenge GDSC GHRCE WEB DEVELOPMENT.pdfSolution Challenge GDSC GHRCE WEB DEVELOPMENT.pdf
Solution Challenge GDSC GHRCE WEB DEVELOPMENT.pdf
 
An Introduction to Cascading Style Sheets (CSS3)
An Introduction to Cascading Style Sheets (CSS3)An Introduction to Cascading Style Sheets (CSS3)
An Introduction to Cascading Style Sheets (CSS3)
 
Compile your Style
Compile your StyleCompile your Style
Compile your Style
 
Architektura html, css i javascript - Jan Kraus
Architektura html, css i javascript - Jan KrausArchitektura html, css i javascript - Jan Kraus
Architektura html, css i javascript - Jan Kraus
 
J query
J queryJ query
J query
 
Webdev bootcamp
Webdev bootcampWebdev bootcamp
Webdev bootcamp
 
HTML5, CSS, JavaScript Style guide and coding conventions
HTML5, CSS, JavaScript Style guide and coding conventionsHTML5, CSS, JavaScript Style guide and coding conventions
HTML5, CSS, JavaScript Style guide and coding conventions
 
Neoito — How modern browsers work
Neoito — How modern browsers workNeoito — How modern browsers work
Neoito — How modern browsers work
 
Intro to CSS Selectors in Drupal
Intro to CSS Selectors in DrupalIntro to CSS Selectors in Drupal
Intro to CSS Selectors in Drupal
 
Training Webinar: Top front-end techniques for OutSystems
 Training Webinar: Top front-end techniques for OutSystems Training Webinar: Top front-end techniques for OutSystems
Training Webinar: Top front-end techniques for OutSystems
 
Top front-end techniques for OutSystems
Top front-end techniques for OutSystemsTop front-end techniques for OutSystems
Top front-end techniques for OutSystems
 
CSS- Smacss Design Rule
CSS- Smacss Design RuleCSS- Smacss Design Rule
CSS- Smacss Design Rule
 
AngularJS Basics
AngularJS BasicsAngularJS Basics
AngularJS Basics
 
Deggan Android Codestyle v1.0
Deggan Android Codestyle v1.0Deggan Android Codestyle v1.0
Deggan Android Codestyle v1.0
 
Css.html
Css.htmlCss.html
Css.html
 
Cascading Style Sheets
Cascading Style SheetsCascading Style Sheets
Cascading Style Sheets
 
Charlotte FED - CSS Inheritance and Specificity
Charlotte FED  - CSS Inheritance and SpecificityCharlotte FED  - CSS Inheritance and Specificity
Charlotte FED - CSS Inheritance and Specificity
 
Drupal front-end performance
Drupal front-end performance Drupal front-end performance
Drupal front-end performance
 
Web Design for Literary Theorists II: Overview of CSS (v 1.0)
Web Design for Literary Theorists II: Overview of CSS (v 1.0)Web Design for Literary Theorists II: Overview of CSS (v 1.0)
Web Design for Literary Theorists II: Overview of CSS (v 1.0)
 

Dernier

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Selectors Performance

  • 2. Types sorted by performance ● Types sorted by performance / souders 1. ID, i.e. #header 2. Class, i.e. .promo 3. Type, i.e. div 4. Adjacent sibling, i.e. h2 + p 5. Child, i.e. div > p 6. Descendant, i.e. ul a 7. Universal, i.e. * 8. Attribute, i.e. [type="text"] 9. Pseudo-classes/-elements, i.e. a:hover
  • 3. Performance impact ● "The impact of CSS selectors on performance derives from the amount of time it takes the browser to match the selector against the elements in the document" Souders
  • 4. How selectors works ● Browsers read selectors right to left ● Example: #div-1 a ○ We read #div-1 with an a ○ Browser reads a in #div-1 ● Why?
  • 5. Example reading left to right document html head body div id:div-1 div id:div-2 a class: nav- link a class: nav- link span a ● div a {... } ● div > span .classX ● div img {...} ● div a img {...} ● #div-1 a {...} ● #div-2 {...} ● #div-1 a span {...} ● div span a {...} ● .class-x a {...} ● .class-z {...} ● div a span {...} ● span a {...} ● div > a {...} ● a.nav-link + a {...}img span
  • 6. Example reading left to right document html head body div id:div-1 div id:div-2 a class: nav- link a class: nav- link span a ● div a {... } ● div > span .classX {..} ● div img {...} ● div a img {...} ● #div-1 a {...} ● #div-2 {...} ● #div-1 a span {...} ● div span a {...} ● .class-x a {...} ● .class-z {...} ● div a span {...} ● span a {...} ● div > a {...} ● a.nav-link + a {...}img span
  • 7. Example reading left to right document html head body div id:div-1 div id:div-2 a class: nav- link a class: nav- link span a ● div a {... } ● div > span .classX {..} ● div img {...} ● div a img {...} ● #div-1 a {...} ● #div-2 {...} ● #div-1 a span {...} ● div span a {...} ● .class-x a {...} ● .class-z {...} ● div a span {...} ● span a {...} ● div > a {...} ● a.nav-link + a {...}img span
  • 8. Example reading right to left document html head body div id:div-1 div id:div-2 a class: nav- link a class: nav- link span a ● div a {... } ● div > span .classX{..} ● div img {...} ● div a img {...} ● #div-1 a {...} ● #div-2 {...} ● #div-1 a span {...} ● div span a {...} ● .class-x a {...} ● .class-z {...} ● div a span {...} ● span a {...} ● div > a {...} ● a.nav-link + a {...}img span
  • 9. Example reading right to left document html head body div id:div-1 div id:div-2 a class: nav- link a class: nav- link span a ● div a {... } ● div > span .classX {..} ● div img {...} ● div a img {...} ● #div-1 a {...} ● #div-2 {...} ● #div-1 a span {...} ● div span a {...} ● .class-x a {...} ● .class-z {...} ● div a span {...} ● span a {...} ● div > a {...} ● a.nav-link + a {...}img span
  • 10. Example reading right to left document html head body div id:div-1 div id:div-2 a class: nav- link a class: nav- link span a ● div a {... } ● div {...} ● div img {...} ● div a img {...} ● #div-1 a {...} ● #div-2 {...} ● #div-1 a span {...} ● div span a {...} ● .class-x a {...} ● .class-z {...} ● div a span {...} ● span a {...} ● div > a {...} ● a.nav-link + a {...}img span
  • 11. How selectors works ● When the browser is trying to style an element has to discard style rules fast ● Starting for the rightmost part of the selector discards a lot of rules at once ● Keep in mind that a common page has hundred of rules ● The rightmost part of the selector is called key selector ● The key selector has a significant impact on the performance of CSS selectors
  • 12. Key Selector ● Rightmost part of the selector ● #some-id .some-class {...} ● div .some-class a {...} ● .some-class * {...} ● The matching work that the browser do is heavily affected by the rightmost selector
  • 13. Tips to efficient selectors ● Avoid universal rules *: ○ Try to avoid universal selectors like *, adjacent sibling, child, descendant and attribute. The recomended selectors are id and class. ● Don't qualify id selectors: ○ There is only one element per id, so there is no need to add additional qualifiers. ● Don't qualify class selectors: ○ Instead of qualifying class selectors for tags, create a new class, ie: a.nav-link use .a-nav-link . ● Specific rules
  • 14. Tips to efficient selectors ● Avoid descendant selectors: ○ They are one of the most expensive to process ● Avoid tag child selectors: ○ tag child selectors like .content > p > a can be replaced by a specific class like .content-anchor ● Use the inheritance: ○ Use the inherited properties, dont repeat. ● The number of rules and the dom deep has an impact in performance
  • 15. Focus on selectors where the key selector matches a large number of elements in the page. ... Too much tips, i lost the focus
  • 16. Tunning selectors ● Example <ul id="nav-links"> <li><a href="http://login.mercadolibre.com.ar">Ingresar</a> </li> <li><a href="http://myaccount.mercadolibre.com.ar/">Mi cuenta</a></li> <li><a href="https://syi.mercadolibre.com.ar/sell">Vender</a></li> <li><a href="http://www.mercadolibre.com.ar/ayuda_home">Ayuda</a></li> </ul> ● #nav-links a {...} will evaluate all a elements and then check if it belongs to an element with nav-links id
  • 17. Tunning selectors ● Example <ul id="nav-links"> <li><a class="nav-link" href="http://login.mercadolibre.com.ar">Ingresar</a> </li> <li><a class="nav-link" href="http://myaccount.mercadolibre.com.ar/">Mi cuenta</a></li> <li><a class="nav-link" href="https://syi.mercadolibre.com.ar/sell" >Vender</a></li> <li><a class="nav-link" href="http://www.mercadolibre.com.ar/ayuda_home" >Ayuda</a></li> </ul> ● .nav-link {...} this will evaluate only the elements with the nav-link class, that are fewer, producing a fast search
  • 18. Side impact ● Selectors not only affect the load time. ● The selectors has an impact on reflows ● Reflows are triggered when DOM element's style properties are modified by javascript. ● Reflows require that the browser re apply the rules, which means match all CSS selectors once again ● Inefficient selectors -> slow reflows -> sluggish page
  • 19. Tradeoff ● Generate more classes and ids to avoid universal selectors add weight to the page. ● More css rules. ● More class and id attributes. ● Less flexibility of the styles. ● Css selector replacing / rewriting is expensive in time and effort.
  • 20. Focus on selectors where the key selector matches a large number of elements in the page. once again!
  • 21. Selectors with Javascript and JQuery ● Different types, kind of convinations -> Different performance
  • 22. Selectors JQuery ● The best: $("#someId") because the native use of document. getElementById() ● Slow: $(".someClassName") because document.getElementsByClassName() is not supported in all browsers IE5-8 ● The worst: $(":pseudo") or $("[attribute=value]") because there is no native calls. in modern browsers querySelector() querySelectorAll() helps
  • 23. Selectors JQuery ● The best: $("#someId") because the native use of document. getElementById() ● Slow: $(".someClassName") because document.getElementsByClassName() is not supported in all browsers IE5-8 ● The worst: $(":pseudo") or $("[attribute=value]") because there is no native calls. in modern browsers querySelector() querySelectorAll() helps
  • 24. Selectors JQuery ● The best: $("#someId") because the native use of document. getElementById() ● Slow: $(".someClassName") because document.getElementsByClassName() is not supported in all browsers IE5-8 ● The worst: $(":pseudo") or $("[attribute=value]") because there is no native calls. in modern browsers querySelector() querySelectorAll() helps
  • 32.
  • 33. JSPerf - Test Case Details
  • 35. JSPerf - Setup & teardown
  • 36. JSPerf - Code snippets
  • 37. JSPerf - Command buttons
  • 38. JSPerf - Test Runner
  • 41. Links ● http://stackoverflow.com/questions/5797014/why-do-browsers-match-css-selectors- from-right-to-left ● http://csswizardry.com/2011/09/writing-efficient-css-selectors/ ● http://www.stevesouders.com/blog/2009/03/10/performance-impact-of-css-selectors/ ● http://www.stevesouders.com/blog/2009/06/18/simplifying-css-selectors/ ● http://answers.oreilly.com/topic/647-how-to-write-efficient-css-selectors/ ● https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Writing_efficient_CSS? redirectlocale=en-US&redirectslug=CSS%2FWriting_Efficient_CSS ● http://www.html5rocks.com/en/tutorials/internals/howbrowserswork ● http://reference.sitepoint.com/css/cascade ● http://caniuse.com/getelementsbyclassname ● http://caniuse.com/queryselector ● Picture: http://www.flickr.com/photos/29008702@N06/5137285917/