SlideShare a Scribd company logo
1 of 55
Download to read offline
JAVASCRIPT
                              SECRETS
                Cleaner Code/ Faster Apps



Wednesday, March 30, 2011
About me
• CTO, Modus                  Create.

     • RIA         UI/UX design

     • High-end              consulting

     • Training             / workshops

     • Architectural            guidance

     • Application             architecture

     • Specialize            in Sencha technologies.

Wednesday, March 30, 2011
Wednesday, March 30, 2011
Wednesday, March 30, 2011
Wednesday, March 30, 2011
My books




Wednesday, March 30, 2011
AGENDA
    • Discuss               some of the secrets behind JavaScript




Wednesday, March 30, 2011
1995-2005
  • Advance                  use of JavaScript was limited until ~ 2005
       • Field          validation
       • Mouse               cursor trails
       • Right              click preventers
       • Popup               storms
  • Lots           of books written
       • Many               taught bad practices



Wednesday, March 30, 2011
Today?
   • JavaScript               frameworks in use more
        • Do         more, quicker!
   • Way            better debug tools
        • Firebug

        • Webkit              inspector
        • IE8        JS debug console (IE9 is a little better)
   • HTML5                  is gaining popularity
        • Flash             is (somewhat) threatened

Wednesday, March 30, 2011
It’s a JavaScript world!




Wednesday, March 30, 2011
Arithmetic Operators




Wednesday, March 30, 2011
Arithmetic
    • Arithmetic            operators

    •+       (add/concatenate)

    •-     (subtract)

    •*      (multiplication)

    •/     (division)

    •%       (modulus/remainder)


Wednesday, March 30, 2011
Arithmetic




Wednesday, March 30, 2011
Remember
    • The         + operator has a dual purpose

        • Addition          and concatenation




Wednesday, March 30, 2011
Operator coercion
    • For        -, *, / operators,

        • JavaScript        will try to convert strings into numbers.




Wednesday, March 30, 2011
•When       at all possible, try to perform
       math using numbers instead of
       strings.
    •This   will help reduce the chance of
       errors.


Wednesday, March 30, 2011
Comparison Operators




Wednesday, March 30, 2011
Comparison operators with
                          if/then

    • Most           of us use if and then to control logic branches.

    • There            is a hidden danger with == and !=

        • Sometimes   the result of an expression test can lead to
            unexpected code behavior



Wednesday, March 30, 2011
Take the following....




Wednesday, March 30, 2011
Meet “Falsy” and “Truthy”

    • Because               JavaScript tries to coerce values, expressions using

        •     == and !=

    • Expressions              are boiled down to “Falsy” and “Truthy”

        • There   should be no room for “Falsy” and “Truthy” in
            your code.


Wednesday, March 30, 2011
Seriously?




Wednesday, March 30, 2011
Takeaway:

                              == and !=
                              are EVIL
                            Do not use them!

Wednesday, March 30, 2011
Instead...

    • Use         === and !==

        • They              are not evil :)

    • Get         expected results every time

    • both          === and !== test for value and data type

        • No          coercion takes place


Wednesday, March 30, 2011
Fight the evil.




                            All are false!
Wednesday, March 30, 2011
Hoisting




Wednesday, March 30, 2011
Hoisting

    •A  mechanism for setting and creating things in a function
       when that function is executed.

        • When     a function is executed, two passes are actually made
            on the function by the JavaScript interpreter.

        • It  can lead to much pain when dealing with function
            statements.


Wednesday, March 30, 2011
How a function really is
              interpreted at execution time




Wednesday, March 30, 2011
How a function really is
              interpreted at execution time




Wednesday, March 30, 2011
Interpretation case 2




Wednesday, March 30, 2011
More on hoisting

    • Due          to hoisting,

        • function          expressions

             • have  their reference name created first and are
                 assigned at execution time.

        • function          statements

             • have  their reference name created and are assigned
                 before execution time

Wednesday, March 30, 2011
Function statement vs. expression




Wednesday, March 30, 2011
Function statement hoisting




Wednesday, March 30, 2011
Function statement hoisting




Wednesday, March 30, 2011
This should be impossible




Wednesday, March 30, 2011
Know hoisting....
    • According  to the hoisting rules, the second jump function
       should have been created

        • Firefox            actually honors the conditional statement - what?!

    • Some             browsers follow these rules and some don’t

    • This is because the language definition doesn’t really tell you
       what it’s supposed to do, so there are different
       implementations.

    • Coding                this way can lead to unpredictable behavior of your

Wednesday, March 30, 2011
Avoid function statements!




Wednesday, March 30, 2011
JS optimizations




Wednesday, March 30, 2011
Asynchronous script tags


    • For        HTML5 enabled browsers

        • Set        async=‘async’ in your script tags.
        •   <script type=‘text/javascript’ src=’/myfile.js’ async=‘async’></script>

        • Will          allow JavaScript files to be non-blocking



Wednesday, March 30, 2011
Deferred execution

    • Set        defer=‘defer’ in your script tags.
        •   <script type=‘text/javascript’ src=’/myfile.js’ defer=‘defer’></script>

    • Will allow JavaScript code in those files to execute after the
       page has loaded.

    • Does            not work in all browsers :(


Wednesday, March 30, 2011
Minification




Wednesday, March 30, 2011
Facts about minification

    • Reduce                file size sent to the browser

    • Increase              interpretation speed of JS files by the browser

    • Some Tools:

        • Yui-Compressor

        • Google             Closure

        • Packer

Wednesday, March 30, 2011
A simple JS File




    • 183         Bytes




Wednesday, March 30, 2011
Minified



    • 103         Bytes

    • 44%          savings




Wednesday, March 30, 2011
A better JS file




    • 184         Bytes


                            Why is it better!?!?
Wednesday, March 30, 2011
‘Better code’ === ‘more savings’



    • 95       Bytes

    • 49%          Savings




Wednesday, March 30, 2011
Loops




Wednesday, March 30, 2011
Loops



      Minifier can't
      claim space


                                     Namespace
                                    lookups costly




Wednesday, March 30, 2011
Faster loops

                                   Minifier friendly




                  No initializer                      Faster Lookup




Wednesday, March 30, 2011
Better reference usage




Wednesday, March 30, 2011
Less than optimal lookups




Wednesday, March 30, 2011
Less than optimal lookups




Wednesday, March 30, 2011
Optimal lookups




Wednesday, March 30, 2011
Recap

    •+   operators will concatenate strings, while -, *, / will coerce
       values

    • Avoid “Truthy” and “Falsy” by      using === and !==

    • Use         async and defer enabled Script tags when possible.

    • Use         function expressions


Wednesday, March 30, 2011
Recap


    • Minify           your code when possible

        • Develop “Minifier-friendly” code

    • Create                optimized loops

    • Reduce                namespace lookups by using local references



Wednesday, March 30, 2011
Thanks!!!
                                Questions?

                                    Twitter: @_jdg
                              Mobile: 301-785-3030
                              Web: moduscreate.com
                            Email: jay@moduscreate.com

Wednesday, March 30, 2011

More Related Content

Viewers also liked

5 new rules for product development
5 new rules for product development5 new rules for product development
5 new rules for product developmentPatrick Sheridan
 
Introducing type script
Introducing type scriptIntroducing type script
Introducing type scriptRemo Jansen
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry Sheiko
 
Modern UI Architecture_ Trends and Technologies in Web Development
Modern UI Architecture_ Trends and Technologies in Web DevelopmentModern UI Architecture_ Trends and Technologies in Web Development
Modern UI Architecture_ Trends and Technologies in Web DevelopmentSuresh Patidar
 
LA ATENCION
LA ATENCIONLA ATENCION
LA ATENCIONCSG
 
Barcelona presentacio tampere antirumors
Barcelona presentacio tampere antirumorsBarcelona presentacio tampere antirumors
Barcelona presentacio tampere antirumorsThomas Jézéquel
 
Nana presentation
Nana presentationNana presentation
Nana presentationcedarcon
 

Viewers also liked (11)

5 new rules for product development
5 new rules for product development5 new rules for product development
5 new rules for product development
 
Introducing type script
Introducing type scriptIntroducing type script
Introducing type script
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Modern UI Architecture_ Trends and Technologies in Web Development
Modern UI Architecture_ Trends and Technologies in Web DevelopmentModern UI Architecture_ Trends and Technologies in Web Development
Modern UI Architecture_ Trends and Technologies in Web Development
 
LA ATENCION
LA ATENCIONLA ATENCION
LA ATENCION
 
Barcelona presentacio tampere antirumors
Barcelona presentacio tampere antirumorsBarcelona presentacio tampere antirumors
Barcelona presentacio tampere antirumors
 
21071
2107121071
21071
 
Sencha Touch in Action
Sencha Touch in Action Sencha Touch in Action
Sencha Touch in Action
 
Teachertube
TeachertubeTeachertube
Teachertube
 
Ciao intro
Ciao introCiao intro
Ciao intro
 
Nana presentation
Nana presentationNana presentation
Nana presentation
 

Similar to JavaScript Secrets

Ruby goes to hollywood
Ruby goes to hollywoodRuby goes to hollywood
Ruby goes to hollywoodehuard
 
Drizzle 7.0, Future of Virtualizing
Drizzle 7.0, Future of VirtualizingDrizzle 7.0, Future of Virtualizing
Drizzle 7.0, Future of VirtualizingBrian Aker
 
Stackbox CMS: Next-Generation Content Management
Stackbox CMS: Next-Generation Content ManagementStackbox CMS: Next-Generation Content Management
Stackbox CMS: Next-Generation Content ManagementVance Lucas
 
Wireframes, User Interfaces, and User Experience
Wireframes, User Interfaces, and User Experience Wireframes, User Interfaces, and User Experience
Wireframes, User Interfaces, and User Experience Erik Eliason
 
Our Best Practices Are Killing Us
Our Best Practices Are Killing UsOur Best Practices Are Killing Us
Our Best Practices Are Killing UsNicole Sullivan
 
Drupal security - Configuration and process
Drupal security - Configuration and processDrupal security - Configuration and process
Drupal security - Configuration and processGábor Hojtsy
 
The State of Front End Web Development 2011
The State of Front End Web Development 2011The State of Front End Web Development 2011
The State of Front End Web Development 2011Pascal Rettig
 
WordPress for ecommerce at wordcamp Indonesia 2011
WordPress for ecommerce at wordcamp Indonesia 2011WordPress for ecommerce at wordcamp Indonesia 2011
WordPress for ecommerce at wordcamp Indonesia 2011Setyagus Sucipto
 
Envato Dev Ops - Alt.Net Melbourne
Envato Dev Ops - Alt.Net MelbourneEnvato Dev Ops - Alt.Net Melbourne
Envato Dev Ops - Alt.Net MelbourneJohn Barton
 
A Brief Intro to Angular.JS
A Brief Intro to Angular.JSA Brief Intro to Angular.JS
A Brief Intro to Angular.JSEric Clemmons
 
NodeJS, CoffeeScript & Real-time Web
NodeJS, CoffeeScript & Real-time WebNodeJS, CoffeeScript & Real-time Web
NodeJS, CoffeeScript & Real-time WebJakub Nesetril
 
The facilities of Features Drupal module
The facilities of Features Drupal moduleThe facilities of Features Drupal module
The facilities of Features Drupal moduleKálmán Hosszu
 
Node js techtalksto
Node js techtalkstoNode js techtalksto
Node js techtalkstoJason Diller
 
Collective Amberjack - European Plone Symposium
Collective Amberjack - European Plone SymposiumCollective Amberjack - European Plone Symposium
Collective Amberjack - European Plone SymposiumMassimo Azzolini
 
Unit testing with Junit
Unit testing with JunitUnit testing with Junit
Unit testing with JunitValerio Maggio
 
CMS Expo 2011 - Social Drupal
CMS Expo 2011 - Social DrupalCMS Expo 2011 - Social Drupal
CMS Expo 2011 - Social DrupalBlake Hall
 

Similar to JavaScript Secrets (20)

Ruby goes to hollywood
Ruby goes to hollywoodRuby goes to hollywood
Ruby goes to hollywood
 
Drizzle 7.0, Future of Virtualizing
Drizzle 7.0, Future of VirtualizingDrizzle 7.0, Future of Virtualizing
Drizzle 7.0, Future of Virtualizing
 
Stackbox CMS: Next-Generation Content Management
Stackbox CMS: Next-Generation Content ManagementStackbox CMS: Next-Generation Content Management
Stackbox CMS: Next-Generation Content Management
 
Nodejs
NodejsNodejs
Nodejs
 
Wireframes, User Interfaces, and User Experience
Wireframes, User Interfaces, and User Experience Wireframes, User Interfaces, and User Experience
Wireframes, User Interfaces, and User Experience
 
Our Best Practices Are Killing Us
Our Best Practices Are Killing UsOur Best Practices Are Killing Us
Our Best Practices Are Killing Us
 
Drupal security - Configuration and process
Drupal security - Configuration and processDrupal security - Configuration and process
Drupal security - Configuration and process
 
The State of Front End Web Development 2011
The State of Front End Web Development 2011The State of Front End Web Development 2011
The State of Front End Web Development 2011
 
Pronk like you mean it
Pronk like you mean itPronk like you mean it
Pronk like you mean it
 
WordPress for ecommerce at wordcamp Indonesia 2011
WordPress for ecommerce at wordcamp Indonesia 2011WordPress for ecommerce at wordcamp Indonesia 2011
WordPress for ecommerce at wordcamp Indonesia 2011
 
Envato Dev Ops - Alt.Net Melbourne
Envato Dev Ops - Alt.Net MelbourneEnvato Dev Ops - Alt.Net Melbourne
Envato Dev Ops - Alt.Net Melbourne
 
Что нового в CSS3
Что нового в CSS3Что нового в CSS3
Что нового в CSS3
 
A Brief Intro to Angular.JS
A Brief Intro to Angular.JSA Brief Intro to Angular.JS
A Brief Intro to Angular.JS
 
Fuck Yeah Nouns
Fuck Yeah NounsFuck Yeah Nouns
Fuck Yeah Nouns
 
NodeJS, CoffeeScript & Real-time Web
NodeJS, CoffeeScript & Real-time WebNodeJS, CoffeeScript & Real-time Web
NodeJS, CoffeeScript & Real-time Web
 
The facilities of Features Drupal module
The facilities of Features Drupal moduleThe facilities of Features Drupal module
The facilities of Features Drupal module
 
Node js techtalksto
Node js techtalkstoNode js techtalksto
Node js techtalksto
 
Collective Amberjack - European Plone Symposium
Collective Amberjack - European Plone SymposiumCollective Amberjack - European Plone Symposium
Collective Amberjack - European Plone Symposium
 
Unit testing with Junit
Unit testing with JunitUnit testing with Junit
Unit testing with Junit
 
CMS Expo 2011 - Social Drupal
CMS Expo 2011 - Social DrupalCMS Expo 2011 - Social Drupal
CMS Expo 2011 - Social Drupal
 

More from Patrick Sheridan

More from Patrick Sheridan (8)

SenchaCon: Sencha Touch Custom Components
SenchaCon: Sencha Touch Custom Components SenchaCon: Sencha Touch Custom Components
SenchaCon: Sencha Touch Custom Components
 
Web audio app preso
Web audio app presoWeb audio app preso
Web audio app preso
 
Rvrsit
RvrsitRvrsit
Rvrsit
 
Javascript Performance Tricks
Javascript Performance TricksJavascript Performance Tricks
Javascript Performance Tricks
 
Discover Music
Discover MusicDiscover Music
Discover Music
 
ExtJS Forms
ExtJS FormsExtJS Forms
ExtJS Forms
 
Intro to sencha touch 2
Intro to sencha touch 2Intro to sencha touch 2
Intro to sencha touch 2
 
Javascript classes and scoping
Javascript classes and scopingJavascript classes and scoping
Javascript classes and scoping
 

Recently uploaded

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 organizationRadu Cotescu
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
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 WorkerThousandEyes
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
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 MenDelhi Call girls
 
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 MenDelhi Call girls
 
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 RobisonAnna Loughnan Colquhoun
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
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 MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 

Recently uploaded (20)

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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
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
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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 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
 
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
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 

JavaScript Secrets

  • 1. JAVASCRIPT SECRETS Cleaner Code/ Faster Apps Wednesday, March 30, 2011
  • 2. About me • CTO, Modus Create. • RIA UI/UX design • High-end consulting • Training / workshops • Architectural guidance • Application architecture • Specialize in Sencha technologies. Wednesday, March 30, 2011
  • 7. AGENDA • Discuss some of the secrets behind JavaScript Wednesday, March 30, 2011
  • 8. 1995-2005 • Advance use of JavaScript was limited until ~ 2005 • Field validation • Mouse cursor trails • Right click preventers • Popup storms • Lots of books written • Many taught bad practices Wednesday, March 30, 2011
  • 9. Today? • JavaScript frameworks in use more • Do more, quicker! • Way better debug tools • Firebug • Webkit inspector • IE8 JS debug console (IE9 is a little better) • HTML5 is gaining popularity • Flash is (somewhat) threatened Wednesday, March 30, 2011
  • 10. It’s a JavaScript world! Wednesday, March 30, 2011
  • 12. Arithmetic • Arithmetic operators •+ (add/concatenate) •- (subtract) •* (multiplication) •/ (division) •% (modulus/remainder) Wednesday, March 30, 2011
  • 14. Remember • The + operator has a dual purpose • Addition and concatenation Wednesday, March 30, 2011
  • 15. Operator coercion • For -, *, / operators, • JavaScript will try to convert strings into numbers. Wednesday, March 30, 2011
  • 16. •When at all possible, try to perform math using numbers instead of strings. •This will help reduce the chance of errors. Wednesday, March 30, 2011
  • 18. Comparison operators with if/then • Most of us use if and then to control logic branches. • There is a hidden danger with == and != • Sometimes the result of an expression test can lead to unexpected code behavior Wednesday, March 30, 2011
  • 20. Meet “Falsy” and “Truthy” • Because JavaScript tries to coerce values, expressions using • == and != • Expressions are boiled down to “Falsy” and “Truthy” • There should be no room for “Falsy” and “Truthy” in your code. Wednesday, March 30, 2011
  • 22. Takeaway: == and != are EVIL Do not use them! Wednesday, March 30, 2011
  • 23. Instead... • Use === and !== • They are not evil :) • Get expected results every time • both === and !== test for value and data type • No coercion takes place Wednesday, March 30, 2011
  • 24. Fight the evil. All are false! Wednesday, March 30, 2011
  • 26. Hoisting •A mechanism for setting and creating things in a function when that function is executed. • When a function is executed, two passes are actually made on the function by the JavaScript interpreter. • It can lead to much pain when dealing with function statements. Wednesday, March 30, 2011
  • 27. How a function really is interpreted at execution time Wednesday, March 30, 2011
  • 28. How a function really is interpreted at execution time Wednesday, March 30, 2011
  • 30. More on hoisting • Due to hoisting, • function expressions • have their reference name created first and are assigned at execution time. • function statements • have their reference name created and are assigned before execution time Wednesday, March 30, 2011
  • 31. Function statement vs. expression Wednesday, March 30, 2011
  • 34. This should be impossible Wednesday, March 30, 2011
  • 35. Know hoisting.... • According to the hoisting rules, the second jump function should have been created • Firefox actually honors the conditional statement - what?! • Some browsers follow these rules and some don’t • This is because the language definition doesn’t really tell you what it’s supposed to do, so there are different implementations. • Coding this way can lead to unpredictable behavior of your Wednesday, March 30, 2011
  • 38. Asynchronous script tags • For HTML5 enabled browsers • Set async=‘async’ in your script tags. • <script type=‘text/javascript’ src=’/myfile.js’ async=‘async’></script> • Will allow JavaScript files to be non-blocking Wednesday, March 30, 2011
  • 39. Deferred execution • Set defer=‘defer’ in your script tags. • <script type=‘text/javascript’ src=’/myfile.js’ defer=‘defer’></script> • Will allow JavaScript code in those files to execute after the page has loaded. • Does not work in all browsers :( Wednesday, March 30, 2011
  • 41. Facts about minification • Reduce file size sent to the browser • Increase interpretation speed of JS files by the browser • Some Tools: • Yui-Compressor • Google Closure • Packer Wednesday, March 30, 2011
  • 42. A simple JS File • 183 Bytes Wednesday, March 30, 2011
  • 43. Minified • 103 Bytes • 44% savings Wednesday, March 30, 2011
  • 44. A better JS file • 184 Bytes Why is it better!?!? Wednesday, March 30, 2011
  • 45. ‘Better code’ === ‘more savings’ • 95 Bytes • 49% Savings Wednesday, March 30, 2011
  • 47. Loops Minifier can't claim space Namespace lookups costly Wednesday, March 30, 2011
  • 48. Faster loops Minifier friendly No initializer Faster Lookup Wednesday, March 30, 2011
  • 50. Less than optimal lookups Wednesday, March 30, 2011
  • 51. Less than optimal lookups Wednesday, March 30, 2011
  • 53. Recap •+ operators will concatenate strings, while -, *, / will coerce values • Avoid “Truthy” and “Falsy” by using === and !== • Use async and defer enabled Script tags when possible. • Use function expressions Wednesday, March 30, 2011
  • 54. Recap • Minify your code when possible • Develop “Minifier-friendly” code • Create optimized loops • Reduce namespace lookups by using local references Wednesday, March 30, 2011
  • 55. Thanks!!! Questions? Twitter: @_jdg Mobile: 301-785-3030 Web: moduscreate.com Email: jay@moduscreate.com Wednesday, March 30, 2011

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n