SlideShare a Scribd company logo
JavaScript
Performance Tricks
   Grgur Grisogono - @ggrgur
Evaluation

eval(foo)
   vs
(new Function(foo))()
Evaluation
                       eval(foo)
                       (new Function(foo))()
6,000
             5,256


4,500


3,000


1,500                       1,081


        97            79              110         160   42         47
   0
        Chrome 21     Firefox 12            IE8              IE7
String Concatenation
                                                   foo = foo + bar
                                                   foo += bar
                                                   [].join(’’)

20,000,000               18,817,419



15,000,000


             9,940,504
10,000,000

                                6,348,293

 5,000,000                                  4,503,838              4,191,713

                                                                                     1,359,961
                                                                               825,543
                                                    435,988   347,348                          443,218
        0
                Chrome 21             Firefox 12          IE9           Safari 5     iOS Safari 5.1.1
String Concatenation
                                      foo = foo + bar
                                      foo += bar
                                      [].join(’’)
                                      foo.concat(bar)
                                                             93,236,430




      56,781,122




  18,817,419             21,484,919


                     4,503,838                                                 5,878,675
                                            1,320,424
                                         347,348         1,493,283          443,218

Chrome 21          Firefox 12            IE9            Safari 5      iOS Safari 5.1.1
String Concatenation
                                            foo = foo + bar
                                            foo += bar
                                            [].join(’’)
                                            foo.concat(bar)
                                                                         93,236,430




            56,781,122




                               21,484,919

9,940,504
                  6,348,293                                  4,191,713                     5,878,675
                                      435,988         1,320,424                1,359,961

    Chrome 21            Firefox 12             IE9                Safari 5       iOS Safari 5.1.1
String Concatenation
String Concatenation
Loops
Loops
                      for loop         while loop

900,000


675,000


450,000


225,000


     0
          Chrome 21              Firefox 12         IE9
Nested Object
 Properties
Nested Object
               Properties
                         deep          shallow

60,000,000


45,000,000


30,000,000


15,000,000


        0
             Chrome 21          Firefox 12       Desktop Safari 5
Reuse Array Refs
                                         foo = [1]
                                         foo.length = 0; foo.push(1)


300,000,000


              221,911,826
225,000,000


150,000,000


 75,000,000

                            10,688,860        10,964,912 18,867,925
                                                                       1,311,286   1,670,314
         0
                   Chrome 21                       Firefox 12                  IE9
Reuse Array Refs
                           foo = [1]
                           foo.length = 0; foo.push(1)


                                          18,867,925                 1,670,314


                                                         1,311,286
221,911,826

                             10,964,912




              10,688,860

    Chrome 21                    Firefox 12                      IE9
Reuse Array Refs
                          foo = [1]
                          foo.length = 0; foo.push(1)


             49,678,120

                                                         2,493,830




                                             1,030,567
14,644,518




 Desktop Safari 5                       Mobile Safari iPhone 4S 5.1.1
Overusing Closures
Minimize Number of
      Events
Minimize Number of
      Events
Timers
0
                                                10 ms timer #1 started
     Time in ms




                                                Screen resized
10                 Main JS code execution
                                                Timer #1 has expired

20

                  Screen resize event handler   10 ms timer #2 started

30
                       Timer #1 handler         Timer #2 has expired


40                     Timer #2 handler         10ms timer #3 started


50

                       Timer #3 handler
Adding Elements To
      DOM
Adding Elements To
      DOM
Adding Elements To
      DOM
Adding Elements To
      DOM
Adding Elements To
               DOM
           innerHTML                  DOM methods/replaceChild
           DOM/cloneNode              DocumentFragment
15,000


11,250


 7,500


 3,750


    0
         Chrome 21 Firefox 12   IE8       Safari    Opera   Mobile Safari
Updating CSS Styles
Updating CSS Styles
                    style            cssText

70,000


52,500


35,000                                                     69,621
                            62,893
           58,388

                                     40,058
17,500


    0
                Chrome 21                     Firefox 12
Selectors
Selectors
      table                                #menu              .first
      li.first                              #menu .first        #menu > li.first
      div#div1 table #menu li.first

1,500,000


1,125,000


 750,000


 375,000


       0
             Chrome 21       Desktop Safari 5    Firefox 12          IE9
Questions?

More Related Content

PDF
The Ring programming language version 1.5.2 book - Part 45 of 181
PDF
The Ring programming language version 1.9 book - Part 207 of 210
PDF
The Ring programming language version 1.5.4 book - Part 53 of 185
PDF
The Ring programming language version 1.5.3 book - Part 47 of 184
KEY
Gotcha! Ruby things that will come back to bite you.
PDF
Leaks & Zombies
PDF
Understanding and visualizing solr explain information - Rafal Kuc
PDF
Coq Tutorial at Proof Summit 2011
The Ring programming language version 1.5.2 book - Part 45 of 181
The Ring programming language version 1.9 book - Part 207 of 210
The Ring programming language version 1.5.4 book - Part 53 of 185
The Ring programming language version 1.5.3 book - Part 47 of 184
Gotcha! Ruby things that will come back to bite you.
Leaks & Zombies
Understanding and visualizing solr explain information - Rafal Kuc
Coq Tutorial at Proof Summit 2011

What's hot (6)

PDF
Coq Tutorial
PDF
Becoming a better developer with EXPLAIN
PDF
League of Graphs
PDF
Deep dive to PostgreSQL Indexes
PDF
A Taste of Python - Devdays Toronto 2009
PDF
Clustering com numpy e cython
Coq Tutorial
Becoming a better developer with EXPLAIN
League of Graphs
Deep dive to PostgreSQL Indexes
A Taste of Python - Devdays Toronto 2009
Clustering com numpy e cython
Ad

Viewers also liked (18)

PDF
The China Analyst March 2011
PPTX
tori . michelle . keenan . austun . kevin
PDF
SenchaCon: DJing with Sencha Touch
PPS
Eenpondverenbloeifactorharrietkollen keywork-1
PPT
Teachertube
PDF
LA ATENCION
 
DOC
Hệ thống ngân hàng thương mại việt nam và những vấn đề đặt ra
PPT
Barcelona presentacio tampere antirumors
PPT
Local Undocumented Migration Policy in the Municipality of Utrecht
PDF
Javascript classes and scoping
PPT
Migrant Integration in Japan
PPTX
Ciao intro
PPTX
Inforln.com ERP LN 10.3 & 10.4 Configurable Contract Deliverables Enhancements
PPT
Irrp pankratova
DOC
Quản trị chiến lược chuẩn
DOC
M1 uf1
PPT
2013 09.9 urbact network workshop compressed
PDF
SenchaCon: Sencha Touch Custom Components
The China Analyst March 2011
tori . michelle . keenan . austun . kevin
SenchaCon: DJing with Sencha Touch
Eenpondverenbloeifactorharrietkollen keywork-1
Teachertube
LA ATENCION
 
Hệ thống ngân hàng thương mại việt nam và những vấn đề đặt ra
Barcelona presentacio tampere antirumors
Local Undocumented Migration Policy in the Municipality of Utrecht
Javascript classes and scoping
Migrant Integration in Japan
Ciao intro
Inforln.com ERP LN 10.3 & 10.4 Configurable Contract Deliverables Enhancements
Irrp pankratova
Quản trị chiến lược chuẩn
M1 uf1
2013 09.9 urbact network workshop compressed
SenchaCon: Sencha Touch Custom Components
Ad

Similar to Javascript Performance Tricks (20)

PDF
PHP and MySQL Tips and tricks, DC 2007
PDF
tutorial5
PDF
tutorial5
PDF
MongoDB, Hadoop and humongous data - MongoSV 2012
PPTX
Top Application Performance Landmines
PDF
Movable type seminar 20120411
PDF
www.webre24h.com - [O`reilly] javascript cookbook - [powers]
PDF
LeMill and Wikiversity: Open Educational Resources
PDF
Django10 cheat-sheet-grayscale
PDF
Distributed computing the Google way
KEY
Big data for the rest of us
PDF
MongoDB Journaling and the Storage Enginer
PPSX
Javascript variables and datatypes
PDF
Php Cookbook Solutions Examples For Php Programmers 3rd David Sklar Adam Trac...
KEY
NoSQL databases and managing big data
ODP
Caching and tuning fun for high scalability @ LOAD2012
ODP
Caching and tuning fun for high scalability @ 4Developers
PDF
Tricking out your Memcached Setup
PDF
02 analysis
PPTX
Cracking the Facebook Coding Interview.pptx
PHP and MySQL Tips and tricks, DC 2007
tutorial5
tutorial5
MongoDB, Hadoop and humongous data - MongoSV 2012
Top Application Performance Landmines
Movable type seminar 20120411
www.webre24h.com - [O`reilly] javascript cookbook - [powers]
LeMill and Wikiversity: Open Educational Resources
Django10 cheat-sheet-grayscale
Distributed computing the Google way
Big data for the rest of us
MongoDB Journaling and the Storage Enginer
Javascript variables and datatypes
Php Cookbook Solutions Examples For Php Programmers 3rd David Sklar Adam Trac...
NoSQL databases and managing big data
Caching and tuning fun for high scalability @ LOAD2012
Caching and tuning fun for high scalability @ 4Developers
Tricking out your Memcached Setup
02 analysis
Cracking the Facebook Coding Interview.pptx

More from Patrick Sheridan (14)

PDF
5 new rules for product development
PDF
SenchaCon: DJing with Sencha Touch
PDF
Sencha Touch in Action
PDF
Web audio app preso
KEY
KEY
Discover Music
KEY
ExtJS Forms
PDF
Intro to sencha touch 2
PDF
HTML5 and Sencha Touch
PPTX
Using Data Visualization to Create Rich Content Experiences - MarkLogic Webin...
PDF
Ext JS 4.0 components and layouts
PDF
Ext JS 4.0 - Creating extensions, plugins and components
PDF
JavaScript Secrets
PDF
Modus Create Corporate Capabilities
5 new rules for product development
SenchaCon: DJing with Sencha Touch
Sencha Touch in Action
Web audio app preso
Discover Music
ExtJS Forms
Intro to sencha touch 2
HTML5 and Sencha Touch
Using Data Visualization to Create Rich Content Experiences - MarkLogic Webin...
Ext JS 4.0 components and layouts
Ext JS 4.0 - Creating extensions, plugins and components
JavaScript Secrets
Modus Create Corporate Capabilities

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPT
Teaching material agriculture food technology
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Electronic commerce courselecture one. Pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
Chapter 3 Spatial Domain Image Processing.pdf
Teaching material agriculture food technology
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Approach and Philosophy of On baking technology
Network Security Unit 5.pdf for BCA BBA.
Mobile App Security Testing_ A Comprehensive Guide.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Electronic commerce courselecture one. Pdf
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25 Week I
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Per capita expenditure prediction using model stacking based on satellite ima...
Understanding_Digital_Forensics_Presentation.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Advanced methodologies resolving dimensionality complications for autism neur...

Javascript Performance Tricks

  • 1. JavaScript Performance Tricks Grgur Grisogono - @ggrgur
  • 2. Evaluation eval(foo) vs (new Function(foo))()
  • 3. Evaluation eval(foo) (new Function(foo))() 6,000 5,256 4,500 3,000 1,500 1,081 97 79 110 160 42 47 0 Chrome 21 Firefox 12 IE8 IE7
  • 4. String Concatenation foo = foo + bar foo += bar [].join(’’) 20,000,000 18,817,419 15,000,000 9,940,504 10,000,000 6,348,293 5,000,000 4,503,838 4,191,713 1,359,961 825,543 435,988 347,348 443,218 0 Chrome 21 Firefox 12 IE9 Safari 5 iOS Safari 5.1.1
  • 5. String Concatenation foo = foo + bar foo += bar [].join(’’) foo.concat(bar) 93,236,430 56,781,122 18,817,419 21,484,919 4,503,838 5,878,675 1,320,424 347,348 1,493,283 443,218 Chrome 21 Firefox 12 IE9 Safari 5 iOS Safari 5.1.1
  • 6. String Concatenation foo = foo + bar foo += bar [].join(’’) foo.concat(bar) 93,236,430 56,781,122 21,484,919 9,940,504 6,348,293 4,191,713 5,878,675 435,988 1,320,424 1,359,961 Chrome 21 Firefox 12 IE9 Safari 5 iOS Safari 5.1.1
  • 10. Loops for loop while loop 900,000 675,000 450,000 225,000 0 Chrome 21 Firefox 12 IE9
  • 12. Nested Object Properties deep shallow 60,000,000 45,000,000 30,000,000 15,000,000 0 Chrome 21 Firefox 12 Desktop Safari 5
  • 13. Reuse Array Refs foo = [1] foo.length = 0; foo.push(1) 300,000,000 221,911,826 225,000,000 150,000,000 75,000,000 10,688,860 10,964,912 18,867,925 1,311,286 1,670,314 0 Chrome 21 Firefox 12 IE9
  • 14. Reuse Array Refs foo = [1] foo.length = 0; foo.push(1) 18,867,925 1,670,314 1,311,286 221,911,826 10,964,912 10,688,860 Chrome 21 Firefox 12 IE9
  • 15. Reuse Array Refs foo = [1] foo.length = 0; foo.push(1) 49,678,120 2,493,830 1,030,567 14,644,518 Desktop Safari 5 Mobile Safari iPhone 4S 5.1.1
  • 19. Timers 0 10 ms timer #1 started Time in ms Screen resized 10 Main JS code execution Timer #1 has expired 20 Screen resize event handler 10 ms timer #2 started 30 Timer #1 handler Timer #2 has expired 40 Timer #2 handler 10ms timer #3 started 50 Timer #3 handler
  • 24. Adding Elements To DOM innerHTML DOM methods/replaceChild DOM/cloneNode DocumentFragment 15,000 11,250 7,500 3,750 0 Chrome 21 Firefox 12 IE8 Safari Opera Mobile Safari
  • 26. Updating CSS Styles style cssText 70,000 52,500 35,000 69,621 62,893 58,388 40,058 17,500 0 Chrome 21 Firefox 12
  • 28. Selectors table #menu .first li.first #menu .first #menu > li.first div#div1 table #menu li.first 1,500,000 1,125,000 750,000 375,000 0 Chrome 21 Desktop Safari 5 Firefox 12 IE9

Editor's Notes

  • #2: About me - MC, Ext JS in Action SE, History, SDC, Sencha \n
  • #3: \n
  • #4: Hard to debug, slow, ... But sometimes necessary (JSON)\nMobile, JSON Decode\n54x faster\nops/sec benchmark.js (jsperf style)\n
  • #5: Dynamic array creation can be slow\n
  • #6: \n
  • #7: \n
  • #8: \n
  • #9: \n
  • #10: a=1 for minimal noise during benchmarks\n
  • #11: \n
  • #12: Straight forward, but let’s examine the impact\n
  • #13: \n
  • #14: \n
  • #15: 2000%\n\n
  • #16: 2000%\n\n
  • #17: \n
  • #18: Bubbling - delegation\nCommon for Ext JS / Sencha Touch DataView/List items\n
  • #19: Bubbling - delegation\nCommon for Ext JS / Sencha Touch DataView/List items\n
  • #20: Fight the unresponsive dialogue\nVisually speed up long tasks (esp with dom manipulation)\n
  • #21: \n
  • #22: \n
  • #23: \n
  • #24: \n
  • #25: \n
  • #26: \n
  • #27: \n
  • #28: \n
  • #29: CSS class selectors are the slowest, ID are the fastest\n
  • #30: \n