Featured image of post 【JavaScript 壓力測試】使用內建函式、chrome devtool 進行壓力測試

【JavaScript 壓力測試】使用內建函式、chrome devtool 進行壓力測試

【JavaScript 壓力測試】使用內建函式、chrome devtool 進行壓力測試

Photo by Mohammad Rahmani on Unsplash

使用內建函式

// test performance function
const testPerformance = (testName, testFunction) => {
    const startTime = new Date().getTime();

    let i = 0;
    while (i < 10000000) {
        i++;
        testFunction();
    }

    const endTime = new Date().getTime();

    // jsonStringify 3698 ms
    console.log(testName, endTime - startTime, 'ms');
};

// 執行測試
testPerformance('jsonStringify', jsonStringify);

// 要測試的功能
function jsonStringify() {
    JSON.stringify({ foo: 'name' });
}
let start;
let end;

start = new Date();

// Do something here
for (var i = 0; i < 1000; i++) {
    Math.sqrt(i);
}

end = new Date();

// Operation took 0 ms
console.log('Operation took ' + (end.getTime() - start.getTime()) + ' ms');

使用 chrome devtool 的函式

const timeTaken = (callback) => {
  console.time('timer');
  const r = callback();
  console.timeEnd('timer');
  return r;
};

timeTaken(() => {
  // Do something here
  for (var i = 0; i < 1000; i++) {
    Math.sqrt(i);
  }
});

參考資料

Donate KJ 贊助作者喝咖啡

如果這篇文章對你有幫助的話,可以透過下面支付方式贊助作者喝咖啡,如果有什麼建議或想說的話可以贊助並留言給我
If this article has been helpful to you, you can support the author by treating them to a coffee through the payment options below. If you have any suggestions or comments, feel free to sponsor and leave a message for me!
方式 Method 贊助 Donate
PayPal https://paypal.me/kejyun
綠界 ECPay https://p.ecpay.com.tw/AC218F1
歐付寶 OPay https://payment.opay.tw/Broadcaster/Donate/BD2BD896029F2155041C8C8FAED3A6F8
All rights reserved,未經允許不得隨意轉載
Built with Hugo
Theme Stack designed by Jimmy