我已经编写了一个脚本用作我的网站的API,但我想提高它的速度。
脚本的重点是计算用户在ROBLOX上所有受限商品的RAP(最近平均价格)的总和。
从“ ROBLOX”用户(RAP总数最大的用户)那里收集到超过4200万RAP需要3.2秒,我想知道是否可以将其降低一到两秒。这是代码:
let request = require('request-promise-lite')
request.Request.defaults = {
json: true
}
const RAP_API = (id, asset, cursor) => `https://inventory.roblox.com/v1/users/${id}/assets/collectibles?assetType=${asset}&sortOrder=Asc&limit=100&cursor=${cursor}`
const assetTypes = ['Hat', 'Gear', 'Face', 'HairAccessory', 'FaceAccessory', 'NeckAccessory', 'ShoulderAccessory', 'FrontAccessory', 'BackAccessory', 'WaistAccessory']
class ROBLOX {
constructor(id) {
this.start = new Date()
this.id = id
this.RAP = 0
}
async getRAPforItemType(asset, cursor = "") {
let API = await request.get(RAP_API(this.id, asset, cursor))
API.data.forEach(item => this.RAP += Number(item.recentAveragePrice) || 0)
if (API.nextPageCursor)
return this.getRAPforItemType(asset, API.nextPageCursor)
}
async getRAP() {
let requests = []
for (let asset of assetTypes)
requests.push(this.getRAPforItemType(asset))
await Promise.all(requests)
return {
RAP: this.RAP,
time: (new Date() - this.start) / 1000
}
}
}
new ROBLOX(1).getRAP().then((RAP) => {
console.log(RAP)
})