SlideShare a Scribd company logo
1 of 50
Download to read offline
JavaScript Engine




                    2011-04-19
• 拔赤
 淘宝前端开发工程师
 http://jayli.github.com
JavaScript引擎
V8
Carakan
JaegerMonkey
Nitro
JScript
从源码到可执行代码


     解释
                编译(compiled)
(interpreted)
(function foo(){
source.js(源码)           alert(‘helloworld’);
                   })();


                                  编译
helloworld.exe         01001011101101…
  (二进制码)
                                  运行

 运行时环境           Runtime.exec(‘helloworld.exe’)




                 编译
(function foo(){
source.js(源码)         alert(‘helloworld’);
                 })();




helloworld.exe                  编译&运行
  (二进制码)


解释器执行伪代码         Runtime.exec(‘中间机器码’)


                      运行时环境

                 解释
(function foo(){
                    alert(‘helloworld’);
               })();



JavaScript引擎
                             编译&运行

               Runtime.exec(‘中间机器码’)


                运行时环境(浏览器)


               解释
JavaScript 是解释型语言
JavaScript代码执行的过程

    词法分析

           语法检查
    语法分析



     预编译
            运行时
     运行
执行”0=1=2”




在语法检查阶段报错
 这是一个语法错误
 程序没有开始运行
执行”a=b=c”




通过了语法检查
 在运行时报错
程序已经开始运行
词法分析

       语法检查
语法分析
              1,将上下文中var申明
                 的变量放入”栈”中
               并赋值为undefined
预编译
        运行时    2,读入”定义”的函数
 运行
alert(a); //会报错吗?
var a = 1;
扫描整段代码,将a放入当前上
               下文的栈中,赋值为undefined

               在栈中找到a并得到值undefined
alert(a);      弹出”undefined”
var a = 1;
               将a赋值为1




             局部变量的预编译
a();
function a(){       2,预编译读入a的定义
  alert(‘Tom’);
}
var a = function(){
  alert(‘Jim’);
                      1,变量a入栈
};
a();

          函数的预编译
a();
function a(){       执行a(),弹出Tom
  alert(‘Tom’);
}
var a = function(){
  alert(‘Jim’);       a被重新赋值
};
a();       执行a(),弹出Jim


           编译后的运行
function a(){//预编译定义,运行时略过
   alert(‘helloworld’);
}
var a = function(){//预编译声明,运行时赋值
   alert(‘helloworld’);
};
a = function(){//运行时变量赋值
  alert(‘helloworld’);
};
下面代码的运行结果?
a();
function a(){
   alert(1);
}
a();
function a(){
   alert(2);
}
a();
var a = function(){
   alert(3);
};
a();
常见问题
判断变量存在

//a未声明时报错,不推荐
alert(a === undefined);



//推荐
alert(typeof a === ‘undefined’);
函数执行前,函数内部变
        量均被声明

function(){
  alert(a); //显示undefined,不报错
  if(false){
     var a = 1; //不会执行到,亦被声明
  }
}()
Js执行和浏览器渲染
<!DOCTYPE HTML>       document
<html lang="zh">
  <head>head</head>            head
<body>
                               body
  <div id=“J”>
     <script>
                                    div#J
     $.write(‘helloworld’);
     </script>                            script

  </div>
…                  渲染出Script节点
</body>
                      浏览器暂停渲染HTML
</html>          将script交由js引擎编译执行
<!DOCTYPE HTML>       document

<html lang="zh">
  <head>head</head>            head

<body>
                               body
  <div id=“J”>
     <script>                       div#J

     $.write(‘helloworld’);
                                            script
     </script>
  </div>         Js引擎创建了
                                          textNode
                   textNode
…
</body>                Js引擎执行代码段结束
</html> 将渲染主动权交给浏览器继续渲染HTML
阻塞:
Js的执行会中断HTML的渲染
Single UI Rendering Thread
Time
                      UI Rendering Thread




        2,UI update

 DOM
1,构建出DOM
Time
                 UI Rendering Thread

RenderUI


     渲染出此时的Dom

 DOM
Time
                      UI Rendering Thread

RenderUI    exec JS

                  3,JS脚本新增了DOM节点

           DOM
Time
                     UI Rendering Thread

RenderUI   exec JS   RenderUI

                        4,UI update

           DOM
Time
                     UI Rendering Thread

RenderUI    UIJS RenderUI
           exec Update
              • Repaint4,UI update

              • Reflow
           DOM
Repaint:
 •   透明度更改
 •   文字颜色变化
 •   背景颜色变化
 •   背景图片替换
Reflow:
•   页面渲染过程中
•   Dom结构变化
•   浏览器窗口大小改变
•   布局变化
<div id=“J”>
  <script>
  $(‘J’).css(‘color’,’red’);
  </script>
</div>                 Repaint
<div id=“J”>
  <script>
  $(‘J’).append(‘<div>txt</div>’);
  </script>
</div>                 Reflow
减少Reflow/paint:性能攸关的大事!
<div id=“J”>
  <script>
  for(i=0;i<100;i++)
     $(‘J’).append(‘<div>’+i+’</div>’);
  </script>
</div>               Reflow * 100 !
Time
                       UI Rendering Thread

RenderUI   Run js for long time   RenderUI

                                     UI update
              UI 没有反应…

               DOM
<div id=“J”>
  <script>
  for(i=0,str=‘’;i<100;i++){
     str+=‘<div>’+i+’</div>’;
  }
  $(‘J’).append(str);
  </script>
</div>              Reflow * 1 !
避免阻塞:同样性能攸关的大事
 (异步执行JavaScript)
…
                         body
<div id=“J”>
  <script>
                              div#J
  setTimeout(function(){
     $.write(‘helloworld’);         script

  },100);
  </script>   Js引擎开启了定时器
</div>
<div>doc</div>
</body>
</html>
           Js引擎只启动了定时器,没有”write”操作
    浏览器可以很快获得DOM渲染权,继续渲染HTML
…
                         body
<div id=“J”>
  <script>
                                div#J
  setTimeout(function(){
     $.write(‘helloworld’);             script

  },100);
                                         div
  </script>
</div>      浏览器继续渲染html
<div>doc</div>
</body>
</html>
…
                         body
<div id=“J”>
  <script>
                              div#J
  setTimeout(function(){
     $.write(‘helloworld’);          script

  },100);
                                      div    100ms
  </script> 定时器到时,插入dom
</div>                              textNode

<div>doc</div>
</body>
</html>          从定时器开启,到write节点完成
                     中间的HTML渲染没有中断
…                       body
<div id=“J”>
  <script>                   div#J

  setTimeout(function(){
                                    script
     while(true){
       $.write(‘helloworld’);        div    100ms

     }
                                   textNode
  },100);    异步也不是万能的
                                       …
  </script>
</div>
…                         Js中低效的Dom操作
依然会阻断浏览器的渲染,让浏览器看起来像“冷冻”住
异步加载JS 减少阻塞
Js不要频繁读写Dom 减少reflow/paint
Lovely JavaScript Engine !




                   了解她
                   喜欢她
                 离不开她
ref

• http://www.slideshare.net/nzakas/high-
  performance-javascript-jquery-conference-
  sf-bay-area-2010-3843763
• http://www.slideshare.net/madrobby/extr
  eme-javascript-performance
JavaScript Engine

More Related Content

What's hot

Unit 1 Revision Booklet
Unit 1 Revision BookletUnit 1 Revision Booklet
Unit 1 Revision BookletZaxapias
 
AS Media Lesson 18 - representation theorists
AS Media Lesson 18 - representation theoristsAS Media Lesson 18 - representation theorists
AS Media Lesson 18 - representation theoristsElle Sullivan
 
Mise en scene of film noir
Mise en scene of film noirMise en scene of film noir
Mise en scene of film noirBarnaby Monahan
 
Roland barthes’s semiotic codes
Roland barthes’s semiotic codesRoland barthes’s semiotic codes
Roland barthes’s semiotic codeskrober4
 
L’image de synthèse au cinéma
L’image de synthèse au cinémaL’image de synthèse au cinéma
L’image de synthèse au cinémaboublyboh
 
Supersize me documentary analysis
Supersize me documentary analysisSupersize me documentary analysis
Supersize me documentary analysisdemibeardmore
 
James bond casino royale - film opening analysis
James bond   casino royale - film opening analysisJames bond   casino royale - film opening analysis
James bond casino royale - film opening analysisharrisonradcliffe
 
Modes of Documentary: Conventions
Modes of Documentary: ConventionsModes of Documentary: Conventions
Modes of Documentary: Conventionsamythechooch
 
Luther - MS4 WJEC Media Case Study- Genre, Narrative and Representation
Luther - MS4 WJEC Media Case Study- Genre, Narrative and RepresentationLuther - MS4 WJEC Media Case Study- Genre, Narrative and Representation
Luther - MS4 WJEC Media Case Study- Genre, Narrative and RepresentationElle Sullivan
 
Analysing the opening of this is england
Analysing the opening of this is englandAnalysing the opening of this is england
Analysing the opening of this is englandhaverstockmedia
 
Representation theory
Representation theoryRepresentation theory
Representation theoryAndy Wallis
 
Radio 1 breakfast show case study
Radio 1 breakfast show case studyRadio 1 breakfast show case study
Radio 1 breakfast show case studyMrs Downie
 
Bill Nichols - 6 Types of Documentary
Bill Nichols - 6 Types of DocumentaryBill Nichols - 6 Types of Documentary
Bill Nichols - 6 Types of DocumentaryEmma Willcox
 
emeli sande heaven analysis
emeli sande heaven analysis emeli sande heaven analysis
emeli sande heaven analysis elleguyan
 
Blank Space by Taylor Swift Analysis
Blank Space by Taylor Swift AnalysisBlank Space by Taylor Swift Analysis
Blank Space by Taylor Swift AnalysisKaylaKosak
 
Scott pilgrim vs the world analysis
Scott pilgrim vs the world analysisScott pilgrim vs the world analysis
Scott pilgrim vs the world analysisdanielle_page
 

What's hot (20)

Unit 1 Revision Booklet
Unit 1 Revision BookletUnit 1 Revision Booklet
Unit 1 Revision Booklet
 
AS Media Lesson 18 - representation theorists
AS Media Lesson 18 - representation theoristsAS Media Lesson 18 - representation theorists
AS Media Lesson 18 - representation theorists
 
Mise en scene of film noir
Mise en scene of film noirMise en scene of film noir
Mise en scene of film noir
 
Roland barthes’s semiotic codes
Roland barthes’s semiotic codesRoland barthes’s semiotic codes
Roland barthes’s semiotic codes
 
L’image de synthèse au cinéma
L’image de synthèse au cinémaL’image de synthèse au cinéma
L’image de synthèse au cinéma
 
Supersize me documentary analysis
Supersize me documentary analysisSupersize me documentary analysis
Supersize me documentary analysis
 
Scream
ScreamScream
Scream
 
James bond casino royale - film opening analysis
James bond   casino royale - film opening analysisJames bond   casino royale - film opening analysis
James bond casino royale - film opening analysis
 
Modes of Documentary: Conventions
Modes of Documentary: ConventionsModes of Documentary: Conventions
Modes of Documentary: Conventions
 
Luther - MS4 WJEC Media Case Study- Genre, Narrative and Representation
Luther - MS4 WJEC Media Case Study- Genre, Narrative and RepresentationLuther - MS4 WJEC Media Case Study- Genre, Narrative and Representation
Luther - MS4 WJEC Media Case Study- Genre, Narrative and Representation
 
Analysing the opening of this is england
Analysing the opening of this is englandAnalysing the opening of this is england
Analysing the opening of this is england
 
Representation theory
Representation theoryRepresentation theory
Representation theory
 
Tv Channel Analysis
Tv Channel AnalysisTv Channel Analysis
Tv Channel Analysis
 
Auteur theory
Auteur theoryAuteur theory
Auteur theory
 
Radio 1 breakfast show case study
Radio 1 breakfast show case studyRadio 1 breakfast show case study
Radio 1 breakfast show case study
 
Bill Nichols - 6 Types of Documentary
Bill Nichols - 6 Types of DocumentaryBill Nichols - 6 Types of Documentary
Bill Nichols - 6 Types of Documentary
 
Genre theory Steve Neale
Genre theory Steve NealeGenre theory Steve Neale
Genre theory Steve Neale
 
emeli sande heaven analysis
emeli sande heaven analysis emeli sande heaven analysis
emeli sande heaven analysis
 
Blank Space by Taylor Swift Analysis
Blank Space by Taylor Swift AnalysisBlank Space by Taylor Swift Analysis
Blank Space by Taylor Swift Analysis
 
Scott pilgrim vs the world analysis
Scott pilgrim vs the world analysisScott pilgrim vs the world analysis
Scott pilgrim vs the world analysis
 

Viewers also liked

犀牛书第六版
犀牛书第六版犀牛书第六版
犀牛书第六版jay li
 
Jswebapps
JswebappsJswebapps
Jswebappsjay li
 
卫银霞 -统计数字会撒谎
卫银霞 -统计数字会撒谎卫银霞 -统计数字会撒谎
卫银霞 -统计数字会撒谎jay li
 
F2e security
F2e securityF2e security
F2e securityjay li
 
中国元素在设计中的应用 -如瑟
中国元素在设计中的应用 -如瑟中国元素在设计中的应用 -如瑟
中国元素在设计中的应用 -如瑟jay li
 
深入剖析浏览器
深入剖析浏览器深入剖析浏览器
深入剖析浏览器jay li
 
潜力无限的编程语言Javascript
潜力无限的编程语言Javascript潜力无限的编程语言Javascript
潜力无限的编程语言Javascriptjay li
 
Responsive Web UI Design
Responsive Web UI DesignResponsive Web UI Design
Responsive Web UI Designjay li
 
淘宝移动端Web开发最佳实践
淘宝移动端Web开发最佳实践淘宝移动端Web开发最佳实践
淘宝移动端Web开发最佳实践jay li
 
淘宝前端技术巡礼
淘宝前端技术巡礼淘宝前端技术巡礼
淘宝前端技术巡礼jay li
 
编码大全 拔赤
编码大全 拔赤编码大全 拔赤
编码大全 拔赤jay li
 
HTML/CSS/JS基础
HTML/CSS/JS基础HTML/CSS/JS基础
HTML/CSS/JS基础jay li
 

Viewers also liked (12)

犀牛书第六版
犀牛书第六版犀牛书第六版
犀牛书第六版
 
Jswebapps
JswebappsJswebapps
Jswebapps
 
卫银霞 -统计数字会撒谎
卫银霞 -统计数字会撒谎卫银霞 -统计数字会撒谎
卫银霞 -统计数字会撒谎
 
F2e security
F2e securityF2e security
F2e security
 
中国元素在设计中的应用 -如瑟
中国元素在设计中的应用 -如瑟中国元素在设计中的应用 -如瑟
中国元素在设计中的应用 -如瑟
 
深入剖析浏览器
深入剖析浏览器深入剖析浏览器
深入剖析浏览器
 
潜力无限的编程语言Javascript
潜力无限的编程语言Javascript潜力无限的编程语言Javascript
潜力无限的编程语言Javascript
 
Responsive Web UI Design
Responsive Web UI DesignResponsive Web UI Design
Responsive Web UI Design
 
淘宝移动端Web开发最佳实践
淘宝移动端Web开发最佳实践淘宝移动端Web开发最佳实践
淘宝移动端Web开发最佳实践
 
淘宝前端技术巡礼
淘宝前端技术巡礼淘宝前端技术巡礼
淘宝前端技术巡礼
 
编码大全 拔赤
编码大全 拔赤编码大全 拔赤
编码大全 拔赤
 
HTML/CSS/JS基础
HTML/CSS/JS基础HTML/CSS/JS基础
HTML/CSS/JS基础
 

Similar to JavaScript Engine

Script with engine
Script with engineScript with engine
Script with engineWebrebuild
 
Web设计 3 java_script初探(程序员与设计师的双重眼光)
Web设计 3 java_script初探(程序员与设计师的双重眼光)Web设计 3 java_script初探(程序员与设计师的双重眼光)
Web设计 3 java_script初探(程序员与设计师的双重眼光)ziggear
 
Javascript autoload
Javascript autoloadJavascript autoload
Javascript autoloadjay li
 
面向开发的前端性能优化
面向开发的前端性能优化面向开发的前端性能优化
面向开发的前端性能优化li qiang
 
Node.js 入門 - 前端工程開發實務訓練
Node.js 入門 - 前端工程開發實務訓練Node.js 入門 - 前端工程開發實務訓練
Node.js 入門 - 前端工程開發實務訓練Joseph Chiang
 
第三方内容开发最佳实践
第三方内容开发最佳实践第三方内容开发最佳实践
第三方内容开发最佳实践taobao.com
 
张所勇:前端开发工具推荐
张所勇:前端开发工具推荐张所勇:前端开发工具推荐
张所勇:前端开发工具推荐zhangsuoyong
 
Kind editor设计思路
Kind editor设计思路Kind editor设计思路
Kind editor设计思路taobao.com
 
Browser Object Model
Browser Object ModelBrowser Object Model
Browser Object Modeljay li
 
浅析浏览器解析和渲染
浅析浏览器解析和渲染浅析浏览器解析和渲染
浅析浏览器解析和渲染Ailsa126
 
中心教员J2 Ee面试题
中心教员J2 Ee面试题中心教员J2 Ee面试题
中心教员J2 Ee面试题yiditushe
 
Kindeditor 设计思路
Kindeditor 设计思路Kindeditor 设计思路
Kindeditor 设计思路luolonghao
 
恶意网页分析实战
恶意网页分析实战恶意网页分析实战
恶意网页分析实战Huang Toby
 
Android 智慧型手機程式設計
Android 智慧型手機程式設計Android 智慧型手機程式設計
Android 智慧型手機程式設計Kyle Lin
 
Inside the-browser
Inside the-browserInside the-browser
Inside the-browserjy03845581
 
Inside the-browser
Inside the-browserInside the-browser
Inside the-browserjy03845581
 
Inside the browser
Inside the browserInside the browser
Inside the browserotakustay
 
Node.js开发体验
Node.js开发体验Node.js开发体验
Node.js开发体验QLeelulu
 
Class 20170126
Class 20170126Class 20170126
Class 20170126Ivan Wei
 
Kindeditor设计思路v2
Kindeditor设计思路v2Kindeditor设计思路v2
Kindeditor设计思路v2luolonghao
 

Similar to JavaScript Engine (20)

Script with engine
Script with engineScript with engine
Script with engine
 
Web设计 3 java_script初探(程序员与设计师的双重眼光)
Web设计 3 java_script初探(程序员与设计师的双重眼光)Web设计 3 java_script初探(程序员与设计师的双重眼光)
Web设计 3 java_script初探(程序员与设计师的双重眼光)
 
Javascript autoload
Javascript autoloadJavascript autoload
Javascript autoload
 
面向开发的前端性能优化
面向开发的前端性能优化面向开发的前端性能优化
面向开发的前端性能优化
 
Node.js 入門 - 前端工程開發實務訓練
Node.js 入門 - 前端工程開發實務訓練Node.js 入門 - 前端工程開發實務訓練
Node.js 入門 - 前端工程開發實務訓練
 
第三方内容开发最佳实践
第三方内容开发最佳实践第三方内容开发最佳实践
第三方内容开发最佳实践
 
张所勇:前端开发工具推荐
张所勇:前端开发工具推荐张所勇:前端开发工具推荐
张所勇:前端开发工具推荐
 
Kind editor设计思路
Kind editor设计思路Kind editor设计思路
Kind editor设计思路
 
Browser Object Model
Browser Object ModelBrowser Object Model
Browser Object Model
 
浅析浏览器解析和渲染
浅析浏览器解析和渲染浅析浏览器解析和渲染
浅析浏览器解析和渲染
 
中心教员J2 Ee面试题
中心教员J2 Ee面试题中心教员J2 Ee面试题
中心教员J2 Ee面试题
 
Kindeditor 设计思路
Kindeditor 设计思路Kindeditor 设计思路
Kindeditor 设计思路
 
恶意网页分析实战
恶意网页分析实战恶意网页分析实战
恶意网页分析实战
 
Android 智慧型手機程式設計
Android 智慧型手機程式設計Android 智慧型手機程式設計
Android 智慧型手機程式設計
 
Inside the-browser
Inside the-browserInside the-browser
Inside the-browser
 
Inside the-browser
Inside the-browserInside the-browser
Inside the-browser
 
Inside the browser
Inside the browserInside the browser
Inside the browser
 
Node.js开发体验
Node.js开发体验Node.js开发体验
Node.js开发体验
 
Class 20170126
Class 20170126Class 20170126
Class 20170126
 
Kindeditor设计思路v2
Kindeditor设计思路v2Kindeditor设计思路v2
Kindeditor设计思路v2
 

More from jay li

小控件、大学问
小控件、大学问小控件、大学问
小控件、大学问jay li
 
Mobile UI design and Developer
Mobile UI design and DeveloperMobile UI design and Developer
Mobile UI design and Developerjay li
 
Html5form
Html5formHtml5form
Html5formjay li
 
Js doc toolkit
Js doc toolkitJs doc toolkit
Js doc toolkitjay li
 
新业务新员工培训 Banner设计
新业务新员工培训   Banner设计新业务新员工培训   Banner设计
新业务新员工培训 Banner设计jay li
 
夏之 专题设计
夏之 专题设计夏之 专题设计
夏之 专题设计jay li
 
赤骥 用户研究入门
赤骥 用户研究入门赤骥 用户研究入门
赤骥 用户研究入门jay li
 
Ecmascript
EcmascriptEcmascript
Ecmascriptjay li
 
2011彩票首页开发实践
2011彩票首页开发实践2011彩票首页开发实践
2011彩票首页开发实践jay li
 
Web设计的画纸深入了解我们的显示器
Web设计的画纸深入了解我们的显示器Web设计的画纸深入了解我们的显示器
Web设计的画纸深入了解我们的显示器jay li
 
潜意识设计
潜意识设计潜意识设计
潜意识设计jay li
 
Html&css培训 舒克
Html&css培训 舒克Html&css培训 舒克
Html&css培训 舒克jay li
 
Html5@taobao
Html5@taobaoHtml5@taobao
Html5@taobaojay li
 
前端调试工具,编码相关,性能相关
前端调试工具,编码相关,性能相关前端调试工具,编码相关,性能相关
前端调试工具,编码相关,性能相关jay li
 
box model
box modelbox model
box modeljay li
 

More from jay li (16)

小控件、大学问
小控件、大学问小控件、大学问
小控件、大学问
 
Mobile UI design and Developer
Mobile UI design and DeveloperMobile UI design and Developer
Mobile UI design and Developer
 
Html5form
Html5formHtml5form
Html5form
 
Slide
SlideSlide
Slide
 
Js doc toolkit
Js doc toolkitJs doc toolkit
Js doc toolkit
 
新业务新员工培训 Banner设计
新业务新员工培训   Banner设计新业务新员工培训   Banner设计
新业务新员工培训 Banner设计
 
夏之 专题设计
夏之 专题设计夏之 专题设计
夏之 专题设计
 
赤骥 用户研究入门
赤骥 用户研究入门赤骥 用户研究入门
赤骥 用户研究入门
 
Ecmascript
EcmascriptEcmascript
Ecmascript
 
2011彩票首页开发实践
2011彩票首页开发实践2011彩票首页开发实践
2011彩票首页开发实践
 
Web设计的画纸深入了解我们的显示器
Web设计的画纸深入了解我们的显示器Web设计的画纸深入了解我们的显示器
Web设计的画纸深入了解我们的显示器
 
潜意识设计
潜意识设计潜意识设计
潜意识设计
 
Html&css培训 舒克
Html&css培训 舒克Html&css培训 舒克
Html&css培训 舒克
 
Html5@taobao
Html5@taobaoHtml5@taobao
Html5@taobao
 
前端调试工具,编码相关,性能相关
前端调试工具,编码相关,性能相关前端调试工具,编码相关,性能相关
前端调试工具,编码相关,性能相关
 
box model
box modelbox model
box model
 

JavaScript Engine