以ä¸ã®ä¸è¡ããã¹ã¦ã® JavaScript ã®åã«èªã¿è¾¼ã
/*@cc_on _d=document;eval('var document=_d')@*/
ãã®ä¸è¡ãèªã¿è¾¼ããã¨ã«ãã£ã¦ IE ã§ã® document ã¸ã®ã¢ã¯ã»ã¹ã 5 åéããªãã¾ãã
ãã¨ãã°
以ä¸ã®ããã«ãèªã¿è¾¼ãåã¨èªã¿è¾¼ãã å¾ãæ¯è¼ãã¦ã¿ã¾ãã
// Before var date = new Date; for (var i = 0; i < 100000; i++) document; alert(new Date - date); // 643 /*@cc_on _d=document;eval('var document=_d')@*/ // After date = new Date; for (var i = 0; i < 100000; i++) document; alert(new Date - date); // 145
éããªã£ã¦ã¾ããï¼
解説
ã¾ãã IE ã§ã¯ document ã«ãã®ã¾ã¾ã¢ã¯ã»ã¹ãã㨠window ãªãã¸ã§ã¯ãã®å
é¨ã¡ã½ãããå®è¡ããã¦ãã¾ãã¾ãããããé常ã«éãã®ã§ãã
ããã§ã以ä¸ã®ããã«ããã¨éããããã¨ãã§ãã¾ãã
var doc = document; document; // ãã£ã¡ãã doc; // ãã£ã¡ã®ãéã
ããã ãã§ãã大ä¸å¤«ãªã®ã§ãããä»ã¾ã§ document ã使ã£ã¦æ¸ãã¦ããã³ã¼ãããã¹ã¦æ¸ãæããã®ã¯ããã©ãããã§ãããã
ã§ãã®ã§
var doc = document; var document = doc;
ã®ãããªãã¨ãåºæ¥ãã°ä¸çªããã®ã§ããããã
ãããã¾ãã JavaScript ã§ã¯å¤æ°ã¯ã¹ã³ã¼ãã®å
é ã§çæãããããã document ã¯ç©ºã®å¤æ°ã¨ãªã undefined ã«ãªã£ã¦ãã¾ãã¾ãã
ããã§ããã«ä»¥ä¸ã®ããã«ãã¾ãã
var doc = document; eval('var document = doc');
eval 㧠var 宣è¨ãããã¨ã§ã¹ã³ã¼ãéä¸ããå¤æ°ãçæãããã¨ãã§ããã®ã§ãã
ããã§ã以éã® document ã¯å
é¨ã¡ã½ãããå¼ã°ããã«å¤æ°ã¨ãã¦ã¢ã¯ã»ã¹ã§ãã¾ãã
æå¾ã«ã IE ã§ããå¹æãªãã®ã§æ¡ä»¶ä»ãã³ã¡ã³ãã§å²ã£ã¦ããã¾ããã
/*@cc_on var doc = document; eval('var document = doc'); @*/
ã¯ããå®æããã¼ãã¼ã±ã¡ã±ã¡ã±ã¡
ã¾ã¨ã
ããã¯ä¾¿å©ï¼ä½¿ã£ã¦ãã ããï¼
追è¨ï¼
ãªããããããå¢ãã§ãã¯ãããã¦ãã®ã§ã³ã£ãããã¤ã¤è¿½è¨ã
以ä¸ã®ããã«ãã㨠document 以å¤ã®ã°ãã¼ãã«å¤æ°ã«ã使ãã¾ããããã¦ãããããã«åãå¹æãããã¾ãã
/*@cc_on eval((function(props) { var code = []; for (var i = 0 l = props.length;i<l;i++){ var prop = props[i]; window['_'+prop]=window[prop]; code.push(prop+'=_'+prop) } return 'var '+code.join(','); })('document self top parent alert setInterval clearInterval setTimeout clearTimeout'.split(' '))); @*/
追è¨ï¼ï¼
ããã«ã®åé¿ã«é©æãã¤ã¤è¿½è¨ï¼ï¼
ãã®æ¹æ³ã¯ document ã¨æ¸ãããé¨åã 5 å以ä¸ï¼ã«ã¼ãã®ã³ã¹ããå¼ãã¨ãã¶ã 10 å以ä¸ï¼ï¼éããããã¨ãã§ããã®ã§ããããã®æ°åã¨ããã®ã¯ãããããã£ã¢ã¯ã»ã¹ãã¨ãé¢æ°å¼ã³åºããã®å·®ã§ãï¼é¢æ°å¼ã³åºããæ¸ããã¦ããã¨èãã¦ãã ããï¼ã
ã§ãã®ã§ãã»ã¨ãã©ã®ã¦ã§ããµã¤ãã§ã¯å¹æã¯ãã¾ãæããããªãããããã¾ããã
ãã ãåã®ä½ã£ã¦ããã®ã§ãããå¹æããã£ãã®ã§ãç¦ã£ã¦ããã°ã«æ¸ãã¦ãã¾ãã¾ãããããã¡ãã£ã¨æ³¨ææ·±ãã¨ã³ããªãæ¸ãã¹ãã ã£ããããããªãã§ããï¼ï¼
åç
§ï¼
一行で IE の JavaScript を高速化する方法 - #生存戦略 、それは - subtech
mala on Twitter: "amachangのあれ、すごいブクマされてるけど10万回ループして0.5秒みたいな話だからね。試しにLDRのツリー描画部分のベンチ取ってみたけど全く速くならなかった。"
ãããããã®æ¹æ³ã¯ document ã ãã«é©ç¨ã§ããæ¹æ³ã§ã¯ããã¾ãããããã¨ãããã DOM ã¢ã¯ã»ã¹ã«å¯¾ãã¦åæ§ã®ãã¯ããã¯ãèãæ¹ãå¿ç¨ãããã¨ãã§ãã¾ããå¿ç¨ããã°ãå¤ãã®ãµã¤ã㧠JS ã®é«éåãåºæ¥ãã®ã§ã¯ãªããã¨æãã¾ãã
ããã辺ã®å
容ã¯ãã®ãã¡æ¸ãã¾ãã
ãé¨ãããã¾ããï¼ï¼