<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 目標 有一個字符串?`var number = '100'`,我們要將它轉換成 Number 類型的 100。 目前有三個選項:+, parseInt, Number 請測試哪個方法更快。 ## [](https://github.com/alsotang/node-lessons/tree/master/lesson10#知識點)知識點 1. 學習使用 benchmark 庫 2. 學習使用?[http://jsperf.com/](http://jsperf.com/)?分享你的 benchmark ## [](https://github.com/alsotang/node-lessons/tree/master/lesson10#課程內容)課程內容 首先去弄個 benchmark 庫,[https://github.com/bestiejs/benchmark.js](https://github.com/bestiejs/benchmark.js)?。 這個庫已經兩年沒有更新了,兩年前發了個 1.0.0 版本,直到現在。 用法也特別簡單,照著官網的 copy 下來就好。 我們先來實現這三個函數: ~~~ var int1 = function (str) { return +str; }; var int2 = function (str) { return parseInt(str, 10); }; var int3 = function (str) { return Number(str); }; ~~~ 然后照著官方的模板寫 benchmark suite: ~~~ var number = '100'; // 添加測試 suite .add('+', function() { int1(number); }) .add('parseInt', function() { int2(number); }) .add('Number', function () { int3(number); }) // 每個測試跑完后,輸出信息 .on('cycle', function(event) { console.log(String(event.target)); }) .on('complete', function() { console.log('Fastest is ' + this.filter('fastest').pluck('name')); }) // 這里的 async 不是 mocha 測試那個 async 的意思,這個選項與它的時間計算有關,默認勾上就好了。 .run({ 'async': true }); ~~~ 直接運行: [![](https://raw.githubusercontent.com/alsotang/node-lessons/master/lesson10/1.png)](https://box.kancloud.cn/2015-08-03_55bf0fa811879.png) 可以看到,parseInt 是最快的。 ### [](https://github.com/alsotang/node-lessons/tree/master/lesson10#在線分享)在線分享 如果想要在線分享你的 js benchmark,用這個網站:[http://jsperf.com/](http://jsperf.com/)?。 比如我在上面測試?`Math.log`?的效率: [http://jsperf.com/math-perf-alsotang](http://jsperf.com/math-perf-alsotang) 進入之后點擊那個?`Run tests`?按鈕,就可以在瀏覽器中看到它們的效率差異了,畢竟瀏覽器也是可以跑 js 的。 點擊這里:[http://jsperf.com/math-perf-alsotang/edit](http://jsperf.com/math-perf-alsotang/edit)?,就可以看到這個 benchmark 是怎么配置的,很簡單。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看