æ§è½è¨æ¸¬ã¤ã³ã¿ã¼ã»ãã¿
ãããã¡ã§ãããã¡ã½ããã®å¼ã³åºãã«ããã£ãæéã表示ããã¤ã³ã¿ã¼ã»ãã¿ãæ¸ãã¦ã¿ã¾ããããã£ãããªã®ã§DIコンテナã«ç»é²ãã¦ä½¿ããããã«ãã¦ãã¾ãã
æ©è½
- ã¡ã½ããã®éå§åã¨éå§å¾ã®æå»ãåå¾ããããªç§åä½ã§å¼ã³åºãã«ããã£ãæéãå¾ãã
- æå»ã¯ãnew Date().getTime()ãã§åå¾ãã¦ããããã精度ã¯Dateã¯ã©ã¹ã®ããã«æºããã
- 5å試è¡ããå¹³åã¨æ大ãæå°ã表示ã
å®è£ ã¨ãµã³ãã«
var stdout = document.getElementById("stdout"); /** * ã¡ã½ããã®å®è¡æéãè¨ãã¤ã³ã¿ã¼ã»ãã¿ * ã¡ã½ããã5åå®è¡ãã¦ãå¹³åãæ大ãæå°ã表示ããã * * @param {Object} mi container.MethodInvocation */ function pref( mi ) { var count = 5; // 試è¡åæ° var total = 0; var max = null; var min = null; for ( var i = 0; i < count; i++ ) { var start = new Date(); try { mi.proceed(); } finally { var time = new Date().getTime() - start.getTime(); total += time; if ( !max || max < time ) { max = time; } if ( !min || min > time ) { min = time; } } } stdout.innerHTML += mi.getMethodName() + " :avg " + Math.floor((total / count)) + "ms, max " + max + "ms, min " + min + "ms,<br/>" ; } // 以ä¸ã¯ãµã³ãã«ã§ãã -------- // è¨æ¸¬å¯¾è±¡ function Logic( ) {} Logic.prototype = { // ã¤ã³ã¯ãªã¡ã³ãã§ã«ã¼ã a: function() { var x = ""; for ( var i = 0; i < 20000; i++ ) { x += "a"; } }, // ãã¯ãªã¡ã³ãã§ã«ã¼ã b: function() { var x = ""; for ( var i = 20000; i >= 0; i-- ) { x += "a"; } } } // ã³ã³ããã®åæå var c = new container.Container( function( binder ) { binder.bind( "logic" ).to( Logic ); // æ§è½è¨æ¸¬ã¤ã³ã¿ã¼ã»ãã¿ãã°ãã¼ãã«ã¤ã³ã¿ã¼ã»ãã¿ã¨ãã¦è¨å® binder.bindInterceptor( pref, container.any(), container.any()); }); // ã¡ã½ãããå¼ã³åºããå¦çæéã表示ãããã var logic = c.get("logic"); logic.a(); logic.b();
グローバルインターセプタã¨ãã¦ç»é²ããã®ã§ãã³ã³ããä¸ã®ãã¹ã¦ã®ã³ã³ãã¼ãã³ãã®ã¡ã½ããå¼ã³åºãã§ãå®è¡æéã表示ããã¾ãã
ã©ãã§ããããã©
Javaã ã¨ãã¯ãªã¡ã³ãã§ã«ã¼ãããæ¹ãæ©ããã¨ãè¨ã話ãã ãã¶åã«èããæ°ããã¾ãããJavaScriptã ã¨ãã¾ãå¤ãããªãã¿ããã§ããã(Javaã®æ¹ãã ãã¶åã®è©±ãªã®ã§ãæè¿ã¯ã©ããç¥ãã¾ãããã)ãã¨ãIE6ã¯ããªãéãã£ããã©ã+=ã®ããããã