js 性能基準測試工具-告別可能、也許、大概這樣更快更省

平時寫js常常遇到這樣作是否是更快點?但又沒有具體簡單可測試的工具,最近也倒序看博客園司徒正美 js分類下的文章html

【ps:去年靈光一閃,發現看博客園排名前100的博客、按照文章分類倒序看是學習最快的方式 O(∩_∩)O~】瀏覽器

看到這篇文章時 (轉)Google Closure: 糟糕的JavaScript http://www.cnblogs.com/rubylouvre/archive/2009/12/07/1615593.htmlruby

文中有些 性能優化對比的舉例,讓我想起去年我尋找js性能基準測試工具、jsdom操做測試工具、js單元測試工具性能優化

今天分享下我找的一個js性能基準測試工具---JSLitmus閉包

JSLitmus是一個輕量級的工具,用於建立針對性的JavaScript基準測試工具dom

JSLitmus 有不少優勢 簡單易用、1分鐘就學會了函數

這是官網地址:http://www.broofa.com/Tools/JSLitmus/工具

官網例子 2 http://www.broofa.com/Tools/JSLitmus/demo_test.html   這個裏面有不少測試用例 O(∩_∩)O~oop

使用JSLitmus 作性能基準測試的基本步驟性能

建立一個靜態頁,引入JSLitmus.js ,而後調用JSLitmus 的基本方法就能夠了 例如

<script src="JSLitmus.js"></script>
<script>
JSLitmus.test('Empty function test', function() {});
</script>

這個空的執行函數會顯示每秒實行無數次

這個函數還能夠傳遞一個參數,官網說的我不是很明白,大概意思是經過傳遞這個參數能夠獲取更加準確的執行結果.,

經過count能夠細節的控制循環,(ps:我的感受就是由本身控制循環…)

JSLitmus.test('a non-looping test', function(count) {
  while (count--) {
        // Your test code goes here
  }
});

 

 

這個性能基準測試工具能夠看到

1 使用內存的狀況,

2多長時間內執行了多少次

我的感受這兩個指標基本夠用了,並且支持多瀏覽器,對與ie瀏覽器,執行時間太長會出現卡死,須要特殊配置,具體配置文章結束再說

這裏能夠看一下官網舉得例子

1 測試對全局變量進行訪問和修改的性能

// First, test a variable in the global scope
var global_var = 1;
JSLitmus.test('global', function(count) {
  while (count--) global_var++;}
);

2測試對局部變量進行訪問和修改的性能

// Now test one that's in a function's local scope
JSLitmus.test('local', function(count) {
  var local_var = 1;
  while (count--) local_var++;
});

3測試在一個閉包中,對上一級變量進行訪問和修改的性能

// Try a variable bound to a closure function.  Prototype and JQuery developers
// should find this particularly interesting.
JSLitmus.test('closure',
  (function() {
    var closure_var = 1;
    return function(count) {while (count--) closure_var++;}
  })()
);

4兩次閉包,最裏面的閉包訪問爺爺級別的變量的性能【ps:做者太奇葩這樣的函數寫出來,不過這裏count咋用我學會了。。】

// Closure binding again, but this time with the variable bound through nested
// closures.
JSLitmus.test('multi-closure',
  (function() {
    var multi_var = 1;
    return (function() {
      return function(count) {while (count--) multi_var++;}
    })()
  })()
);

5測試對一個引用空函數表達式的調用

// Test an empty function call, which we can use as a reference point
JSLitmus.test('empty function call', function(count) {
  var f = function() {};
  while (count--) f();
});
 

image

相關文章
相關標籤/搜索