SlideShare una empresa de Scribd logo
1 de 105
Descargar para leer sin conexión
Extreme
JavaScript
Performance
Thomas Fuchs
@thomasfuchs
DO NOT, EVER,
OPTIMIZE
PREMATURELY
http://tr.im/extremejs
SpiderMonkey
SpiderMonkey
JavaScriptCore
SpiderMonkey
JavaScriptCore
JScript
SpiderMonkey
JavaScriptCore
JScript
V8
#1
avoid function calls
function  methodCall(){
    function  square(n){  return  n*n  };
    var  i=10000,  sum  =  0;
    while(i-­‐-­‐)  sum  +=  square(i);
}
function  inlinedMethod(){  
    var  i=10000,  sum  =  0;
    while(i-­‐-­‐)  sum  +=  i*i;
}
function  methodCall(){
    function  square(n){  return  n*n  };
    var  i=10000,  sum  =  0;
    while(i-­‐-­‐)  sum  +=  square(i);
}
function  inlinedMethod(){  
    var  i=10000,  sum  =  0;
    while(i-­‐-­‐)  sum  +=  i*i;
}
function  methodCall(){
    function  square(n){  return  n*n  };
    var  i=10000,  sum  =  0;
    while(i-­‐-­‐)  sum  +=  square(i);
}
function  inlinedMethod(){  
    var  i=10000,  sum  =  0;
    while(i-­‐-­‐)  sum  +=  i*i;
}
methodCall() inlinedMethod()
0.410s 0.150s
0.056s 0.045s
uhm,  hmm† 0.128s
0.027s 0.016s
methodCall() inlinedMethod()
0.410s 0.150s
0.056s 0.045s
uhm,  hmm† 0.128s
0.027s 0.016s
methodCall() inlinedMethod()
0.410s 0.150s
0.056s 0.045s
uhm,  hmm† 0.128s
0.027s 0.016s
methodCall() inlinedMethod()
0.410s 0.150s
0.056s 0.045s
uhm,  hmm† 0.128s
0.027s 0.016s
methodCall() inlinedMethod()
0.410s 0.150s
0.056s 0.045s
uhm,  hmm† 0.128s
0.027s 0.016s
IE8 throws this warning
after 1 second
#2
embrace the
language
function  literals(){
    var  a  =  [],  o  =  {};
}
function  classic(){
    var  a  =  new  Array,  o  =  new  Object;
}
classic() literals()
0.291s 0.265s
0.020s 0.016s
0.220s 0.185s
0.024s 0.010s
classic() literals()
0.291s 0.265s
0.020s 0.016s
0.220s 0.185s
0.024s 0.010s
classic() literals()
0.291s 0.265s
0.020s 0.016s
0.220s 0.185s
0.024s 0.010s
classic() literals()
0.291s 0.265s
0.020s 0.016s
0.220s 0.185s
0.024s 0.010s
classic() literals()
0.291s 0.265s
0.020s 0.016s
0.220s 0.185s
0.024s 0.010s
>  parseInt(12.5);
12
>  ~~(1  *  "12.5")
12
1 * string coerces the
string into a float,
result = 12.5
double bitwise NOT*
floors the number
>  ~~(1  *  "12.5")
12
*good overview on http://tr.im/bitwise
parseInt() weird  stuff
0.003s 0.002s
0.088s 0.081s
uhm,  hmm† 0.547s
0.109s 0.282s
parseInt() weird  stuff
0.003s 0.002s
0.088s 0.081s
uhm,  hmm† 0.547s
0.109s 0.282s
parseInt() weird  stuff
0.003s 0.002s
0.088s 0.081s
uhm,  hmm† 0.547s
0.109s 0.282s
parseInt() weird  stuff
0.003s 0.002s
0.088s 0.081s
uhm,  hmm† 0.547s
0.109s 0.282s
parseInt() weird  stuff
0.003s 0.002s
0.088s 0.081s
uhm,  hmm† 0.547s
0.109s 0.282s
parseInt() weird  stuff
0.003s 0.002s
0.088s 0.081s
uhm,  hmm† 0.547s
0.109s 0.282s
Firefox is 30x faster
than Safari
#3
loops
var  test  =  '';
for  (var  i  =  0;i<10000;i++)
  test  =  test  +  str;
var  test  =  '',  i  =  10000;
while(i-­‐-­‐)  test  =  test  +  str;
for  loop while  loop
0.12s 0.12s
0.13s 0.13s
0.6s 0.6s
0.04s 0.04s
for  loop while  loop
0.12s 0.12s
0.13s 0.13s
0.6s 0.6s
0.04s 0.04s
for  loop while  loop
0.12s 0.12s
0.13s 0.13s
0.6s 0.6s
0.04s 0.04s
for  loop while  loop
0.12s 0.12s
0.13s 0.13s
0.6s 0.6s
0.04s 0.04s
for  loop while  loop
0.12s 0.12s
0.13s 0.13s
0.6s 0.6s
0.04s 0.04s
var  test  =  '';
for  (var  i  =  0;i<10000;i++)
  test  =  test  +  str;
var  test  =  '',  i  =  10000;
while(i-­‐-­‐)  test  =  test  +  str;
3 expressions in “for”
1 expression in “while”
(when i equals 0, expression will be false)
function  normalLoop(){
    var  i=60,  j=0;
    while(i-­‐-­‐)  j++;
}
function  unrolledLoop(){
    var  j=0;
    j++;  j++;  j++;  j++;  j++;  j++;  
    j++;  j++;  j++;  j++;  j++;  j++;
    j++;  j++;  j++;  j++;  j++;  j++;
    j++;  j++;  j++;  j++;  j++;  j++;
    j++;  j++;  j++;  j++;  j++;  j++;
    j++;  j++;  j++;  j++;  j++;  j++;
    j++;  j++;  j++;  j++;  j++;  j++;
    j++;  j++;  j++;  j++;  j++;  j++;
    j++;  j++;  j++;  j++;  j++;  j++;
    j++;  j++;  j++;  j++;  j++;  j++;
}
normalLoop() unrolledLoop()
0.023s 0.010s
0.003s 0.001s
0.032s 0.015s
0.005s 0.001s
normalLoop() unrolledLoop()
0.023s 0.010s
0.003s 0.001s
0.032s 0.015s
0.005s 0.001s
normalLoop() unrolledLoop()
0.023s 0.010s
0.003s 0.001s
0.032s 0.015s
0.005s 0.001s
normalLoop() unrolledLoop()
0.023s 0.010s
0.003s 0.001s
0.032s 0.015s
0.005s 0.001s
normalLoop() unrolledLoop()
0.023s 0.010s
0.003s 0.001s
0.032s 0.015s
0.005s 0.001s
#4
cache globals
function  uncached(){
    var  i  =  10000;
    while(i-­‐-­‐)  window.test  =  'test';
}
function  cached(){
    var  w  =  window,  i  =  10000;
    while(i-­‐-­‐)  w.test  =  'test';
}
uncached cached
1.440s 0.825s
0.07s 0.07s
2.22s 2.19s
0.48s 0.16s
uncached cached
1.440s 0.825s
0.07s 0.07s
2.22s 2.19s
0.48s 0.16s
uncached cached
1.440s 0.825s
0.07s 0.07s
2.22s 2.19s
0.48s 0.16s
uncached cached
1.440s 0.825s
0.07s 0.07s
2.22s 2.19s
0.48s 0.16s
uncached cached
1.440s 0.825s
0.07s 0.07s
2.22s 2.19s
0.48s 0.16s
uncached cached
1.440s 0.825s
0.07s 0.07s
2.22s 2.19s
0.48s 0.16s
Safari is 20x faster
than Firefox
uncached cached
1.440s 0.825s
0.07s 0.07s
2.22s 2.19s
0.48s 0.16s
Now IE works with >1s durations. WTF?
#5
expression tuning
var  b  =  false,  n  =  99;
function(){
    return  n*n  &&  b;
}
function(){
    return  b  &&  n*n;
}
var  b  =  false,  n  =  99;
function(){
    return  n*n  &&  b;
}
function(){
    return  b  &&  n*n;
} b is false,
so n*n doesn’t need
to get evaluated
not  tuned tuned
0.005s 0.004s
0.011s 0.010s
0.906s 0.391s
0.037s 0.021s
not  tuned tuned
0.005s 0.004s
0.011s 0.010s
0.906s 0.391s
0.037s 0.021s
not  tuned tuned
0.005s 0.004s
0.011s 0.010s
0.906s 0.391s
0.037s 0.021s
not  tuned tuned
0.005s 0.004s
0.011s 0.010s
0.906s 0.391s
0.037s 0.021s
not  tuned tuned
0.005s 0.004s
0.011s 0.010s
0.906s 0.391s
0.037s 0.021s
>>>  var  n  =  1;
undefined
>>>  if(true  &&  (n=2))  ...;
>>>  n
2
>>>  if(true  ||  (n=3))  ...;
>>>  n
2
not a pure engine optimization,
the execution actually stops
here, n=2 needs to
be evaluated,
so n is set to 2
here it doesn’t
(expression must
be true), so n is
NOT set to 3
#6
what not to use
function(){
    var  obj  =  {  prop:  'test',  str:  ''  };
    with(obj){  
        var  i  =  10000;
        while(i-­‐-­‐)  str  +=  prop;
        return  str;
    }
}
function(){
    var  obj  =  {  prop:  'test',  str:  ''  },  i  =  10000;
    while(i-­‐-­‐)  obj.str  +=  obj.prop;
    return  obj.str;
}
with(obj){  p  } obj.p
0.071s 0.012s
0.039s 0.028s
0.078s 0.078s
0.077s 0.006s
with(obj){  p  } obj.p
0.071s 0.012s
0.039s 0.028s
0.078s 0.078s
0.077s 0.006s
with(obj){  p  } obj.p
0.071s 0.012s
0.039s 0.028s
0.078s 0.078s
0.077s 0.006s
with(obj){  p  } obj.p
0.071s 0.012s
0.039s 0.028s
0.078s 0.078s
0.077s 0.006s
with(obj){  p  } obj.p
0.071s 0.012s
0.039s 0.028s
0.078s 0.078s
0.077s 0.006s
var  a  =  0;
function(){
    try{
        a  +=  1;
    }  catch(e)  {}
}
function(){
    a  +=  1;
}
try/catch no  try/catch
0.006s 0.005s
0.287s 0.011s
0.460s 0.460s
0.123s 0.012s
try/catch no  try/catch
0.006s 0.005s
0.287s 0.011s
0.460s 0.460s
0.123s 0.012s
try/catch no  try/catch
0.006s 0.005s
0.287s 0.011s
0.460s 0.460s
0.123s 0.012s
try/catch no  try/catch
0.006s 0.005s
0.287s 0.011s
0.460s 0.460s
0.123s 0.012s
try/catch no  try/catch
0.006s 0.005s
0.287s 0.011s
0.460s 0.460s
0.123s 0.012s
Firefox 3.5
Safari 4.0
Chrome 3
IE 8
0 0,1 0,2 0,3 0,4 0,5
no try/catch try/catch
Firefox 3.5
Safari 4.0
Chrome 3
IE 8
0 0,1 0,2 0,3 0,4 0,5
no try/catch try/catch
Firefox 3.5
Safari 4.0
Chrome 3
IE 8
0 0,1 0,2 0,3 0,4 0,5
no try/catch try/catch
Firefox 3.5
Safari 4.0
Chrome 3
IE 8
0 0,1 0,2 0,3 0,4 0,5
no try/catch try/catch
Firefox 3.5
Safari 4.0
Chrome 3
IE 8
0 0,1 0,2 0,3 0,4 0,5
no try/catch try/catch
Modern JavaScript
engines have JIT
compilers, which don’t
support certain
features well
Avoid stuff
that’s not
available in
ECMA-262
5th Edition
“strict” mode,
see John’s blog
post
Avoid stuff
that’s not
available in
ECMA-262
5th Edition
“strict” mode,
see John’s blog
post
http://tr.im/ecma262
alert((function(){return"alert
(("+arguments.callee.toString()
.replace(/s/g,"")+")());";})());
alert((function(){return"alert
(("+arguments.callee.toString()
.replace(/s/g,"")+")());";})());
(function(){  return  2  *  3;  }).toString();
function  ()  {  return  2  *  3;  }
function  ()  {  return  2  *  3;  }
function  ()  {  return  2  *  3;  }
function  ()  {  return  2  *  3;  }
function  ()  {  return  2  *  3;  }
function  ()  {  return  2  *  3;  }
function  ()  {  return  2  *  3;  }
function  ()  {  return  2  *  3;  }
function  ()  {  return  2  *  3;  }
function  ()  {  return  6;  }
function  ()  {  return  2  *  3;  }
function  ()  {  return  2  *  3;  }
function  ()  {  return  2  *  3;  }
function  ()  {  return  6;  } WTF?
More in
http://jsrocks.com
More in
http://jsrocks.com
DO NOT, EVER,
OPTIMIZE
PREMATURELY
Q&A
And thanks!
http://javascriptrocks.com/
@thomasfuchs on twitter

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Js: master prototypes
Js: master prototypesJs: master prototypes
Js: master prototypes
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
 
#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기
 
Javaチョットデキルへの道〜JavaコアSDKに見る真似したいコード10選〜
Javaチョットデキルへの道〜JavaコアSDKに見る真似したいコード10選〜Javaチョットデキルへの道〜JavaコアSDKに見る真似したいコード10選〜
Javaチョットデキルへの道〜JavaコアSDKに見る真似したいコード10選〜
 
[오픈소스컨설팅] jira service desk 201908
[오픈소스컨설팅] jira service desk 201908[오픈소스컨설팅] jira service desk 201908
[오픈소스컨설팅] jira service desk 201908
 
(알도개) GraalVM – 자바를 넘어선 새로운 시작의 서막
(알도개) GraalVM – 자바를 넘어선 새로운 시작의 서막(알도개) GraalVM – 자바를 넘어선 새로운 시작의 서막
(알도개) GraalVM – 자바를 넘어선 새로운 시작의 서막
 
Introduction to Return-Oriented Exploitation on ARM64 - Billy Ellis
Introduction to Return-Oriented Exploitation on ARM64 - Billy EllisIntroduction to Return-Oriented Exploitation on ARM64 - Billy Ellis
Introduction to Return-Oriented Exploitation on ARM64 - Billy Ellis
 
Ksug2015 - JPA2, JPA 기초와매핑
Ksug2015 - JPA2, JPA 기초와매핑Ksug2015 - JPA2, JPA 기초와매핑
Ksug2015 - JPA2, JPA 기초와매핑
 
multi-thread 어플리케이션에 대해 모든 개발자가 알아 두지 않으면 안 되는 것
multi-thread 어플리케이션에 대해 모든 개발자가 알아 두지 않으면 안 되는 것multi-thread 어플리케이션에 대해 모든 개발자가 알아 두지 않으면 안 되는 것
multi-thread 어플리케이션에 대해 모든 개발자가 알아 두지 않으면 안 되는 것
 
SpringDataJPA - 스프링 캠프
SpringDataJPA - 스프링 캠프SpringDataJPA - 스프링 캠프
SpringDataJPA - 스프링 캠프
 
Jdk9で変更になる(かも知れない)jvmオプションの標準設定
Jdk9で変更になる(かも知れない)jvmオプションの標準設定Jdk9で変更になる(かも知れない)jvmオプションの標準設定
Jdk9で変更になる(かも知れない)jvmオプションの標準設定
 
いまどきの OAuth / OpenID Connect (OIDC) 一挙おさらい (2020 年 2 月) #authlete
いまどきの OAuth / OpenID Connect (OIDC) 一挙おさらい (2020 年 2 月) #authleteいまどきの OAuth / OpenID Connect (OIDC) 一挙おさらい (2020 年 2 月) #authlete
いまどきの OAuth / OpenID Connect (OIDC) 一挙おさらい (2020 年 2 月) #authlete
 
DDD로 복잡함 다루기
DDD로 복잡함 다루기DDD로 복잡함 다루기
DDD로 복잡함 다루기
 
JPA For Beginner's
JPA For Beginner'sJPA For Beginner's
JPA For Beginner's
 
[2018] MyBatis에서 JPA로
[2018] MyBatis에서 JPA로[2018] MyBatis에서 JPA로
[2018] MyBatis에서 JPA로
 
Grails GORM - You Know SQL. You Know Queries. Here's GORM.
Grails GORM - You Know SQL. You Know Queries. Here's GORM.Grails GORM - You Know SQL. You Know Queries. Here's GORM.
Grails GORM - You Know SQL. You Know Queries. Here's GORM.
 
Hibernate in Action
Hibernate in ActionHibernate in Action
Hibernate in Action
 
Jpa
JpaJpa
Jpa
 
What is tackled in the Java EE Security API (Java EE 8)
What is tackled in the Java EE Security API (Java EE 8)What is tackled in the Java EE Security API (Java EE 8)
What is tackled in the Java EE Security API (Java EE 8)
 
Testing with JUnit 5 and Spring - Spring I/O 2022
Testing with JUnit 5 and Spring - Spring I/O 2022Testing with JUnit 5 and Spring - Spring I/O 2022
Testing with JUnit 5 and Spring - Spring I/O 2022
 

Destacado

jQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & CompressionjQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & Compression
Paul Irish
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax Applications
Julien Lecomte
 
Texto sobre la Consulta Previa/Conamaq-Cidob
Texto sobre la Consulta Previa/Conamaq-CidobTexto sobre la Consulta Previa/Conamaq-Cidob
Texto sobre la Consulta Previa/Conamaq-Cidob
somossur
 

Destacado (18)

Using jsPerf correctly
Using jsPerf correctlyUsing jsPerf correctly
Using jsPerf correctly
 
jQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & CompressionjQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & Compression
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax Applications
 
iOS Coding Best Practices
iOS Coding Best PracticesiOS Coding Best Practices
iOS Coding Best Practices
 
Know yourengines velocity2011
Know yourengines velocity2011Know yourengines velocity2011
Know yourengines velocity2011
 
An Educators Guide to Podcasting and Broadcasting by @TeacherCast
An Educators Guide to Podcasting and Broadcasting by @TeacherCastAn Educators Guide to Podcasting and Broadcasting by @TeacherCast
An Educators Guide to Podcasting and Broadcasting by @TeacherCast
 
Al-Huda CIBE- 4th African Islamic Finance Summit
Al-Huda CIBE- 4th African Islamic Finance SummitAl-Huda CIBE- 4th African Islamic Finance Summit
Al-Huda CIBE- 4th African Islamic Finance Summit
 
Job interview techniques
Job interview techniques Job interview techniques
Job interview techniques
 
India’s women bankers at the helm
India’s women bankers at the helmIndia’s women bankers at the helm
India’s women bankers at the helm
 
Compliance
ComplianceCompliance
Compliance
 
Obama chronicles the book
Obama chronicles the bookObama chronicles the book
Obama chronicles the book
 
Modelo kata de competencia digital. isdi
Modelo kata de competencia digital. isdiModelo kata de competencia digital. isdi
Modelo kata de competencia digital. isdi
 
Turing machine
Turing  machine Turing  machine
Turing machine
 
ガチでビジネス DALIを使った照明制御
ガチでビジネス DALIを使った照明制御ガチでビジネス DALIを使った照明制御
ガチでビジネス DALIを使った照明制御
 
Samad Oraee - Learn More About Foot Pain
Samad Oraee - Learn More About Foot PainSamad Oraee - Learn More About Foot Pain
Samad Oraee - Learn More About Foot Pain
 
Texto sobre la Consulta Previa/Conamaq-Cidob
Texto sobre la Consulta Previa/Conamaq-CidobTexto sobre la Consulta Previa/Conamaq-Cidob
Texto sobre la Consulta Previa/Conamaq-Cidob
 
Boletín 08/03/2017
Boletín 08/03/2017Boletín 08/03/2017
Boletín 08/03/2017
 
Come creare e gestire campagne Google AdWords per E-Commerce | La settimana d...
Come creare e gestire campagne Google AdWords per E-Commerce | La settimana d...Come creare e gestire campagne Google AdWords per E-Commerce | La settimana d...
Come creare e gestire campagne Google AdWords per E-Commerce | La settimana d...
 

Más de Thomas Fuchs

Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2KZepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Thomas Fuchs
 

Más de Thomas Fuchs (9)

Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-FrameworksZepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
 
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2KZepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
 
I Can't Believe It's Not Flash
I Can't Believe It's Not FlashI Can't Believe It's Not Flash
I Can't Believe It's Not Flash
 
Prototype & Scriptaculous
Prototype  & ScriptaculousPrototype  & Scriptaculous
Prototype & Scriptaculous
 
Textorize
TextorizeTextorize
Textorize
 
Adventures In JavaScript Testing
Adventures In JavaScript TestingAdventures In JavaScript Testing
Adventures In JavaScript Testing
 
Ruby On Rails Introduction
Ruby On Rails IntroductionRuby On Rails Introduction
Ruby On Rails Introduction
 
Twistori Tech
Twistori TechTwistori Tech
Twistori Tech
 
Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)
 

Extreme JavaScript Performance