Google Apps Script 運行速度不佳嗎?
一直覺得 Google Apps Script 寫出來的程式在組資料時速度不佳,直到今天想說用個簡單的測試來跑跑看…
參與的選手
- Google Apps Script 免費
- Oracle VPS 免費: AMD EPYC 7551 32-Core Processor 2 個 AMD 型運算 VM (各有 1/8 個 OCPU** 和 1 GB 記憶體),大概 2 核心 * 0.125 可使用
- 我的電腦 要錢:AMD Ryzen 5 2400G
Google Apps Script 跑 for loop 塞 obj 測試
// 數值加總
function forSumTest() {
let start = Date.now();
let sum = 0
for (let i = 0; i < 1000000000; i++) {
sum += i
}
Logger.log(`加總值為:${sum}`)
let end = Date.now();
Logger.log(`Execution time: ${end - start} ms`);
}
// 1705 + 1666 + 1075 + 1598 + 1604 + 1683 + 1528 + 1459 + 1093 + 1023 = 14,434 ms
// 平均 1,443.4 ms
// obj 賦值
function forObjTest() {
let sum = 0
let iRound = 10
let jRound = 100000000
for (let i = 0; i < iRound; i++) {
let start = Date.now();
let obj = {}
for (let j = 0; j < jRound; j++) {
obj[j] = j
}
let end = Date.now();
Logger.log(`Execution time: ${end - start} ms`);
sum += end - start
}
Logger.log(`Avg time: ${sum / iRound} ms`);
}
// 平均 5312.3 ms
Oracle VPS Node.js 環境裝起來
安裝 nvm 工具來管理 Node.js
$ curl https://raw.githubusercontent.com/creationix/nvm/v0.39.3/install.sh | bash
$ source ~/.bash_profile
查看有哪些版本可以安裝
$ nvm ls-remote
...
v16.19.0 (Latest LTS: Gallium)
...
v18.14.0 (Latest LTS: Hydrogen)
...
v19.6.0
裝最新的 LTS 版
$ nvm install v18.14.0
or
$ nvm install --lts
查看
$ nvm list
切換版本(如有裝其他版本)
$ nvm use v16.19
切換最新的 LTS 版本
$ nvm use --lts
設定預設版本(如有裝其他版本)
$ nvm alias default v16.19
Oracle VPS 跑 for loop 塞 obj 測試
// 數字加總
let start = Date.now();
let sum = 0
for (let i = 0; i < 1000000000; i++) {
sum += i
}
console.log(`加總值為:${sum}`)
let end = Date.now();
console.log(`Execution time: ${end - start} ms`);
// Oracle VPS
// 3273 + 3141 + 3667 + 3197 + 3203 + 3192 + 3197 + 3183 + 3722 + 3203 = 32,978 ms
// 平均 3,297.8 ms
// obj 賦值
let sum = 0
let iRound = 10
let jRound = 100000000
for (let i = 0; i < iRound; i++) {
let start = Date.now();
let obj = {}
for (let j = 0; j < jRound; j++) {
obj[j] = j
}
let end = Date.now();
console.log(`Execution time: ${end - start} ms`);
sum += end - start
}
console.log(`Avg time: ${sum / iRound} ms`);
// JavaScript heap out of memory 炸了
AMD Ryzen5 2400G 跑 for loop 塞 obj 測試
let start = Date.now();
let sum = 0
for (let i = 0; i < 1000000000; i++) {
sum += i
}
console.log(`加總值為:${sum}`)
let end = Date.now();
console.log(`Execution time: ${end - start} ms`);
// AMD 2400G 效能模式
// 858 + 868 + 857 + 900 + 862 + 858 + 860 + 869 + 859 + 870 = 8,661 ms
// 平均 866.1 ms
let sum = 0
let iRound = 10
let jRound = 100000000
for (let i = 0; i < iRound; i++) {
let start = Date.now();
let obj = {}
for (let j = 0; j < jRound; j++) {
obj[j] = j
}
let end = Date.now();
console.log(`Execution time: ${end - start} ms`);
sum += end - start
}
console.log(`Avg time: ${sum / iRound} ms`);
// 平均 3174.5 ms
大出意料之外,是我 Code 寫不好,錯怪他了。但發布出來的 Web App 前端在調用 GAS 不做任何邏輯直接 return 出來還是會有 700 ~ 1,000 ms 的等待 response 時間,GAS 讀試算表 400 筆資料不做任何處理要 300 ms ~ 700 ms,在初始頁面時也慢,適合寫工具就好。
Google Apps Script | Oracle VPS | AMD Ryzen5 2400G | |
---|---|---|---|
加總 | 1,443.4 ms | 3,297.8 ms | 866.1 ms |
obj 賦值 | 5312.3 ms | — 炸了 — | 3174.5 ms |