SlideShare uma empresa Scribd logo
1 de 41
Baixar para ler offline
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/

Mais conteúdo relacionado

Semelhante a Selectors Performance

Semelhante a 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)
 

Último

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Último (20)

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
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

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/